首页 > 解决方案 > Grep 匹配模式但排除特定字符串

问题描述

我有包含各种日志行的文件,我想要grep一个模式但排除 when 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"

标签: greppattern-matching

解决方案


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

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


推荐阅读