grep记录的最后2分钟? [英] grep last 2 minutes of log?
问题描述
我有以下格式的日志文件. 我想每2分钟对日志进行一次grep检查并检查错误 使用shell脚本我怎么能得到这个? 任何帮助将不胜感激.
I have log file that is generated in below format. I want to grep the log every 2 minutes and check for error using shell script how Can I get this? Any help would be greatly appreciated.
[12:17:02.1274]
[12:18:01.1976]
[12:18:01.2151]
[12:18:01.2152]
[12:18:01.4607]
[12:18:02.6306]
[12:18:03.7299]
[12:18:04.0307]
[12:18:04.1388]
[12:18:04.2068]
[12:18:06.4002]
[12:18:07.5805]
[12:19:01.8559]
[12:19:08.9950]
[12:19:09.2851]
[12:19:10.4704]
[12:19:12.8167]
[12:20:01.8968]
[12:20:01.9206]
[12:20:01.9206]
[12:20:02.1707]
[12:20:02.3064]
[12:20:05.4461]
[12:20:13.7205]
[12:20:14.0807]
[12:20:14.2360]
[12:20:17.3764]
[12:21:01.2299]
[12:21:10.4769]
[12:21:18.8085]
[12:21:19.1106]
[12:21:19.2456]
[12:21:22.3663]
推荐答案
您可以使用睡眠功能.
尝试例如:
#!/bin/bash
echo start
sleep 2
echo end
两个回显命令之间将等待2秒.
Which will wait 2 seconds between the two echo commands.
您的完整脚本可能是:
#!/bin/bash
while [ 1 ]; do
sleep 2m
if grep -qvE "^\[[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{4}\]" logfile.dat
then
echo "ERROR"
else
echo "Everything is fine..."
fi
done
while循环始终在运行,因为1
是true
.在此无限循环中,每两分钟(2m
)完成一次检查.
The while-loop is always running because 1
is true
. In this infinite loop each two minutes (2m
) the check is done.
修改
我更新了代码,以便解析给定的数据结构.我将grep
与
I updated the code so that the data structure you were given is parsed. I use grep
with
-
-E
以启用正则表达式 -
-v
仅匹配反函数(这意味着它搜索您的结构,并且如果此模式不不匹配,则实际上是grepping) -
-q
使用安静模式并在找到匹配项后立即退出
-E
to enable the usage of regex-v
to match only the inverse (meaning that it searches for your structure and is actually grepping if this pattern isn't matching)-q
to use quiet mode and exit immediately if a match is found
如果您的 logfile.dat 与您给出的样式不完全相同,则grep匹配,并且将 ERROR 写入通常是您终端的STDOUT
.
If your logfile.dat is not exactly of the style you were giving, grep is matching and ERROR is written to STDOUT
which typically is your terminal.
这篇关于grep记录的最后2分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!