微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

linux-替换所有不包含匹配字符串的行

我正在使用如下所示的数据文件

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

我正在尝试使用sed将不包含字符串hello的所有行替换为match,因此输出为:

match
hello random text in file
match
words in file hello
match
hello text in file can be
match

我尝试使用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

/你好/!确保我们仅在不包含hello的行(您拥有该权利)上进行替换,然后替换将完整的模式空间(.*)替换为match.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐