假设我有一个名为data.log的文件。
数据不断地追加到其中,其中可能包含“ flag ”,并且被外部脚本截断:
[2015年10月13日:11:11:53 +0000]欧盟中的聊天标志裙软核
尾部:data.log:文件被截断
[2015年10月13日:11:11:53 +0000]箭头产生蛋白质
[2015年10月13日:11:11:54 +0000]不是目标标记和底池。
声明之后counter=0
,我想增加它与发现的事件的数量。我想出了类似这样的东西,它wc -l
用来计数data.log中包含' flag '的行:
counter=$(($counter+$(cat data.log | grep s | wc -l)))
echo $counter
现在只剩下一个问题:截断。我将如何处理?我曾考虑过进行watch
,但是如何处理截断事件呢?还是我应该朝另一个方向前进?
这应该为您工作:
$ tail -F data.log | grep --line-buffered s | while read match ; do ((counter++)) ; done
的-F
标志tail
与相同--follow --retry
。--retry
这里是神奇的地方:
--retry
keep trying to open a file even when it is or becomes inaccessi‐
ble; useful when following by name, i.e., with --follow=name
我还--line-buffered
与一起使用grep
,以避免while循环需要等待输出。
我刚刚意识到的一个问题:如果标志对于每行出现多次,$counter
仍然只会增加1。但是这个问题也在您的解决方案中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句