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.*Has purge"
部分我正在通过,在一个循环中,所以我无法通过-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.*已爆炸"将匹配除 1.2.3.4 之外的任何其他内容的 syslog 行,告诉我它已经爆炸了.我必须能够包含不匹配的 IP.
I am confused trying to do the inverse of the above, and 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.
我在 StackOverflow 上看到过各种类似的帖子.然而,他们使用我似乎无法使用 grep
的正则表达式模式.任何人都可以为 grep
提供一个工作示例吗?
I have seen various similar posts on StackOverflow. 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屋!