问题
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