Linux 仅当当前行中存在字符串时才替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11958369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:05:01  来源:igfitidea点击:

Replace only if string exists in current line

linuxshellsed

提问by Anders Lind

I have a line such as:

我有一条线,例如:

sed -i 's/mystring/newstring/' $target

This command will change all mystringto newstring.

此命令将全部更改mystringnewstring.

What I want now is: when the program sees mystring, how can I check for the current line if the string searchstringexists or not? If it exists, newstringis 1; otherwise, newstringis 0.

我现在想要的是:当程序看到时mystring,如何检查当前行是否searchstring存在字符串?如果存在,newstring则为1;否则,newstring0

采纳答案by Ihor Kaharlichenko

Solution

解决方案

Assuming your input file $target contains the following:

假设您的输入文件 $target 包含以下内容:

some text mystring some other text
some text mystring a searchstring
just some more text

This command:

这个命令:

sed -i -e '/searchstring/ s/mystring/1/ ; /searchstring/! s/mystring/0/' $target

will change its content to:

将其内容更改为:

some text 0 some other text
some text 1 a searchstring
just some more text

Explanation

解释

The script contains two substitute (s) commands separated by a semicolon.

该脚本包含两个用分号分隔的替代 ( s) 命令。

The substitute command accepts an optional address range that select which lines the substitution should take place.

替换命令接受一个可选的地址范围,用于选择应该进行替换的行。

In this case regexpaddress was used to select lines containing the searchstringfor the first command; and the lines that do not contain the searchstring(note the exclamation mark after the regexp negating the match) for the second one.

在这种情况下,regexpaddress 用于选择包含第一个命令的搜索字符串的行;以及不包含第二个搜索字符串的行(注意正则表达式否定匹配后的感叹号)。

Edit

编辑

This command will perform better and produce just the same result:

此命令将执行得更好并产生相同的结果:

sed -i -e '/searchstring/ s/mystring/1/ ; s/mystring/0/' $target

The point is that commands are executed sequentially and thus if there is still a mystringsubstring in the current line afterthe first command finished then there is no searchstringin it for sure.

的一点是,如果仍有一个命令顺序,因此执行了mystring在当前行子串之后完成所述第一命令则没有searchString的在它是肯定的。

Kudos to user946850.

感谢 user946850。

回答by krlmlr

This is from the sed one-linerspage:

这是来自sed one-liners页面:

OPTIMIZING FOR SPEED: If execution speed needs to be increased (due to large input files or slow processors or hard disks), substitution will be executed more quickly if the "find" expression is specified before giving the "s/.../.../" instruction. Thus:

sed 's/foo/bar/g' filename         # standard replace command
sed '/foo/ s/foo/bar/g' filename   # executes more quickly
sed '/foo/ s//bar/g' filename      # shorthand sed syntax

优化速度:如果需要提高执行速度(由于输入文件大或处理器或硬盘速度慢),如果在给出“s/.../. ../“ 操作说明。因此:

sed 's/foo/bar/g' filename         # standard replace command
sed '/foo/ s/foo/bar/g' filename   # executes more quickly
sed '/foo/ s//bar/g' filename      # shorthand sed syntax

Speed is not the issue of the question at hand, but the syntax hints help formulating the solution:

速度不是手头问题的问题,但语法提示有助于制定解决方案:

sed -i '/searchstring/ s/mystring/1/; s/mystring/0/' $target