sed creates duplicate line instead of replacing ex

2019-07-12 20:19发布

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.

2条回答
相关推荐>>
2楼-- · 2019-07-12 20:51

If you like to try awk

awk '/some-text/ {$2=1} 1' file
查看更多
Root(大扎)
3楼-- · 2019-07-12 21:10

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) ?

查看更多
登录 后发表回答