简体   繁体   English

sed与特定字符串一致

[英]Sed on line with specific string

I am having trouble with sed utility. 我在使用sed实用程序时遇到了麻烦。 I have to substitute regex with a string but only on lines which contains another specific string. 我必须用字符串代替正则表达式,但只能在包含另一个特定字符串的行上使用。 Whats more I have to read from file and then modify it by sed , not only export substitute values. 而且,我还必须从文件中读取内容,然后通过sed对其进行修改,不仅要导出替代值。 Here is what I have tried: 这是我尝试过的:

sed -e '`grep aa test`"s/aa/xx/g test'

sed -n '/aa/p' test | sed s/aa/xx/g

grep aa test | sed 's/aa/xx/g'

Use an adress before s command : s命令之前使用地址:

sed '/aa/ s/aa/xx/g' test

s command will only apply on lines containing the address pattern s命令仅适用于包含地址模式的行

But there is no need to have the same pattern in address and in s command. 但是,不需要在address和s命令中使用相同的模式。

A better example would be : 一个更好的例子是:

sed '/bb/ s/aa/xx/g' test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM