Grep用于匹配模式,但排除特定的字符串

时间:2019-05-16 02:39:09

标签: grep pattern-matching

我的文件包含各种日志行,我想grep选择一个模式,但是排除message:com.mycompany.excluded的时间,因此基本上应该返回以下内容:

"The log found this message blah, message:com.blahblah"
"The log found this message blah2, message:com.blahblah2"
"The log found this message foobar, message:com.mycompany"
"The log found this message blah, message:com.mycompany.included"

但不是:

 "The log found this message blah, message:com.mycompany.excluded"

我正在使用此模式,但不能排除com.mycompany.excluded

grep "The log found this message.*message:.*" "mylogs.txt"

2 个答案:

答案 0 :(得分:1)

awk '/The log found this message.*message:/ && !/com.mycompany.excluded/' "mylogs.txt"

有多种方法可以使它更健壮,但这并不是必需的(取决于日志文件其余部分的外观)。

答案 1 :(得分:0)

您要的是sed,而不是grep

sed -i .bak 's/^.*excluded//' mylogs.txt

-i .bak创建扩展名为.bak的原始文件的备份;这将使您分别拥有mylogs.txtmylogs.txt.bak,其中mylogs.txt具有删除不需要的行。)

相关问题