Dom*_*red 2 linux bash awk sed
我正在处理这个看起来像这样的数据文件:
text in file
hello random text in file
example text in file
words in file hello
more words in file
hello text in file can be
more text in file
Run Code Online (Sandbox Code Playgroud)
我想,以取代那些所有行不包含字符串hello与match使用SED,所以输出将是:
match
hello random text in file
match
words in file hello
match
hello text in file can be
match
Run Code Online (Sandbox Code Playgroud)
我尝试使用sed '/hello/!d'但删除了该行.另外,我读到我可以!在sed 中使用匹配,但我不确定如何匹配每一行并正确替换.如果你可以给我一些方向,我会非常感激.
您可以这样做:
$ sed '/hello/!s/.*/match/' infile
match
hello random text in file
match
words in file hello
match
hello text in file can be
match
Run Code Online (Sandbox Code Playgroud)
/hello/!确保我们仅在不包含的行上进行替换hello(您拥有该权利),然后替换用替换完整的模式空间(.*)match。
awk 救援!
$ awk '!/hello/{$0="match"}1' file
Run Code Online (Sandbox Code Playgroud)
将与"hello"匹配的行替换为"match"并打印所有行.
使用c(change) 命令sed :
$ sed '/hello/!c match' file
match
hello random text in file
match
words in file hello
match
hello text in file can be
match
Run Code Online (Sandbox Code Playgroud)