正则表达式匹配X字符串是否包含单词?

时间:2013-03-13 18:14:16

标签: regex unix regex-negation monit

我正在处理一个我不知道是否有可能以这种方式解决的问题,希望你能帮助我,让我们看看:

我正在使用Monit来监控一些日志文件,我希望它能够查找某个表达式,但如果该表达式出现的次数超过,我只想要一个肯定的匹配,比方说3次。

有问题的日志文件如下所示:

[2013/03/12-16:07:06]   Configurator :: FillSensor()
[2013/03/12-16:07:06]   [SEVERE]     :: Exception was caught: Could not bind to port.. Exiting.
[2013/03/12-16:07:06]   Configurator :: FillSensor()
[2013/03/12-16:07:06]   Configurator :: FillSensor()
[2013/03/12-16:07:06]   [SEVERE]     :: Exception was caught: Could not bind to port.. Exiting.
[2013/03/12-16:07:06]   Configurator :: FillSensor()
[2013/03/12-16:07:06]   Configurator :: FillSensor()
[2013/03/12-16:07:06]   Configurator :: FillSensor()
[2013/03/12-16:07:06]   Configurator :: FillSensor()
[2013/03/12-16:07:06]   [SEVERE]     :: Exception was caught: Could not bind to port.. Exiting.
[2013/03/12-16:07:06]   Configurator :: FillSensor()
[2013/03/12-16:07:06]   Configurator :: FillSensor()
[2013/03/12-16:07:06]   Configurator :: FillSensor()
[2013/03/12-16:07:06]   Configurator :: FillSensor()
[2013/03/12-16:07:06]   Configurator :: FillSensor()
[2013/03/12-16:07:06]   Configurator :: FillSensor()
[2013/03/12-16:07:06]   [SEVERE]     :: Exception was caught: Could not bind to port.. Exiting.
[2013/03/12-16:07:06]   Configurator :: FillSensor()

我正在寻找“ [严重] ”表达。

如果该表达式出现超过3次,我想要一个匹配。

我知道.*\[SEVERE\].*给了我与该表达式匹配的所有行,但我想只匹配该行数是3或更多。有没有办法用正则表达式做到这一点?或者也许可以选择使用Monit吗?

1 个答案:

答案 0 :(得分:2)

如果您可以在功能强大的shell中访问日志文件:

[ $(cat LOG.txt | grep "\[SEVERE\]" | wc -l) -ge 3 ]

这将文件的内容传递给grep,它搜索包含“[SEVERE]”的行,然后wc计算行数,如果行数大于或,则表达式返回0等于3。