2012-02-09 26 views
0

mysql的日誌執行某些命令有一樣的東西:掃描實時文件,並在遇到連續5個相同的行

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腳本,將做follwoing:

  1. 的腳本將掃描文件mysql.log實時(類似tail -F mysql.log
  2. 如果遇到連續5個相同的行那麼將調用一些命令(比如echo "yes"

回答

2
awk '$0 != l { l=$0; c=0 } 
     l=$0 {c++; if (c>=5) {system("YOURCOMMAND HERE")}}' LOGFILE 

可以爲你做它...但你通過實時是什麼意思?如果它必須不斷運行,爲它設置一些fifo機制,或類似的東西。或者根本:

tail -F LOGFILE | <THEABOVE_AWK_SCRIPT> 
+0

通過實時我的意思,該腳本將不斷運行,掃描mysql.log(像尾-f mysql.log |同時readline的) – user973430 2012-02-09 12:34:44

+1

謝謝.. :)最好使用「尾巴 - F「 – user973430 2012-02-09 13:05:48

+0

@ user973430你是對的,更正了。如果您認爲:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – 2012-02-09 14:22:03