实时扫描文件,并在遇到连续5行相同的行时执行某些命令



Mysql日志有类似的内容:

1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1

我想要一个bash-shell脚本,它将执行以下操作:

  1. 脚本将实时扫描mysql.log文件(类似于tail -F mysql.log
  2. 如果它遇到5个连续的相同行,那么它将调用一些命令(比如echo "yes"
 awk '$0 != l { l=$0; c=0 }
      l=$0 {c++; if (c>=5) {system("YOURCOMMAND HERE")}}' LOGFILE

可以为你做。。。但你说的实时是什么意思?如果它必须不断运行,请为它设置一些fifo机制,或者类似的机制。或者简单地做:

 tail -F LOGFILE | <THEABOVE_AWK_SCRIPT>

最新更新