sed创建重复的行,而不是替换现有的行 [英] sed creates duplicate line instead of replacing existing line

查看:46
本文介绍了sed创建重复的行,而不是替换现有的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的文件(foo.txt):

I have a file (foo.txt) containing the following:

some-text 0

我使用以下sed命令将0替换为1:

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;

这将导致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?

它发生在SL06上的GNU sed 4.2.1版本中.

It occurs with GNU sed version 4.2.1 on SL06.

推荐答案

您尝试

search_text='some-text'
sed -e "s/\(${search_text}\) 0/\1 1/" -i foo.txt

使用组模式而不是两次search_text

using group pattern instead of twice the search_text

您使用的是哪个shell(因为我看到; 之类的 c 行尾在外壳中的多行中很少使用)?

which shell are you using (cause i see ; like c end of line not often used on several line in shell) ?

这篇关于sed创建重复的行,而不是替换现有的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆