Grep正则表达式不包含字符串 [英] Grep regex NOT containing string
问题描述
我将正则表达式列表传递给 grep
以检查系统日志文件。它们通常与IP地址和日志条目匹配;
I am passing a list of regex patterns to grep
to check against a syslog file. They are usually matching an IP address and log entry;
grep "1\.2\.3\.4.*Has exploded" syslog.log
它只是一个模式列表,如1 \.2\.3\.4。*已爆炸
部分我正在传递,在一个循环中,因此我无法传递-v作为示例。
It's just a list of patterns like the "1\.2\.3\.4.*Has exploded"
part I am passing, in a loop, so I can't pass "-v" for example.
我很困惑,试着做上面的反转,一个不匹配的行与某个IP地址和错误,所以!1.2.3.4。* has exploded会匹配除1.2.3.4以外的其他任何系统日志行告诉我它已经发生了爆炸。我必须可以包含一个不匹配的IP。
I am confused trying to do the inverse of the above, an NOT match lines with a certain IP address and error so "!1.2.3.4.*Has exploded" will match syslog lines for anything other than 1.2.3.4 telling me it has exploded. I must be able to include an IP to NOT match.
我在StackOverflor上看到过不同的类似帖子,但他们使用了我可以使用的正则表达式似乎无法与 grep
一起工作。任何人都可以提供一个适用于 grep
的示例吗?
I have seen various similar posts on StackOverflor however they use regex patterns that I can't seem to get to work with grep
. Can anyone provide a working example for grep
please?
更新:
这发生在这样的脚本中;
UPDATE: This is happening in a script like this;
patterns[1]="1\.2\.3\.4.*Has exploded"
patterns[2]="5\.6\.7\.8.*Has died"
patterns[3]="\!9\.10\.11\.12.*Has exploded"
for i in {1..3}
do
grep "${patterns[$i]}" logfile.log
done
推荐答案
grep
匹配, grep -v
做相反的处理。如果您需要匹配A而不是B,您通常会使用管道:
grep
matches, grep -v
does the inverse. If you need to "match A but not B" you usually use pipes:
grep "${PATT}" file | grep -v "${NOTPATT}"
这篇关于Grep正则表达式不包含字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!