问题
I have a file (foo.txt) containing the following:
some-text 0
I use the following sed-command to replace the 0 with a 1:
search_text="some-text";
sed "s/${search_text} 0/${search_text} 1/" -i foo.txt;
This results in foo.txt containing:
some-text 0
some-text 1
How can I get it to replace the found line instead of appending a new line?
It occurs with GNU sed version 4.2.1 on SL06.
回答1:
If you like to try awk
awk '/some-text/ {$2=1} 1' file
回答2:
do you try
search_text='some-text'
sed -e "s/\(${search_text}\) 0/\1 1/" -i foo.txt
using group pattern instead of twice the search_text
which shell are you using (cause i see ;
like c
end of line not often used on several line in shell) ?
来源:https://stackoverflow.com/questions/23734146/sed-creates-duplicate-line-instead-of-replacing-existing-line