sed creates duplicate line instead of replacing existing line

久未见 提交于 2020-01-15 04:29:27

问题


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

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