I'm trying to replace a string in a specific line number using a variable

安稳与你 提交于 2021-01-28 05:01:05

问题


here's my attempt:

sed -i -e "$chs/|0|/|900|/" users

The variable "chs" has the line number in which I want to replace the string, but it doesn't work, any ideas?


回答1:


If chs contains the line number, say 5, then the following:

sed -i -e "$chs/|0|/|900|/" users

will expand to:

sed -i -e "5/|0|/|900|/" users

As a sed command, that is nonsense. It should return an error message about an unknown command.

You need to create a substitute command preceded by a line number. Try:

sed -i -e "$chs s/|0|/|900|/" users



回答2:


awk to the rescue!

 awk -v n="$chs" 'NR==n{sub(/search/,replace)}1' 


来源:https://stackoverflow.com/questions/33458003/im-trying-to-replace-a-string-in-a-specific-line-number-using-a-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!