开发者

How to count with grep or awk, pattern if it occurred x times respectively (one after the other) in last 10 lines of file

开发者 https://www.devze.com 2023-03-03 10:23 出处:网络
I need help with little code. What I need is \"if statement\" code the counts with grep or other command how many times repeat respectively two words in the file.

I need help with little code. What I need is "if statement" code the counts with grep or other command how many times repeat respectively two words in the file. The contest of file is:

2011/05/04 21:47:37 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - CHANNEL 1 (0,8W) (invalid SID)
2011/05/04 21:47:46 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - 0000:0000 unknown (invalid SID)
2011/05/04 21:48:11 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL
2011/05/04 21:48:21 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:48:42 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - 0000:1000 unknown (invalid SID)
2011/05/04 21:49:19 45992299 c blabla (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:49:30 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL CHANNEL (0,8
2011/05/04 21:49:32 45992299 c blabla (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:50:01 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:50:18 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - 0000:0333 unknown (invalid SID)
2011/05/04 21:50:21 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - 0000:0333 unknown (invalid SID)
2011/05/04 21:51:06 45992299 c blabla (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:51:08 45992299 c blabla (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:51:18 45992299 c blabla (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL
2011/05/04 21:52:19 45992299 c blabla (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:52:45 45992299 c blabla (xxx/xxx/xx:xxx): not found (2008 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:53:56 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:53:58 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2008 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:01 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2008 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:04 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/0开发者_运维知识库4 21:54:07 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:10 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:13 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:15 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:18 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2011 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:20 45992299 c blabla (xxx/xxx/xx:xxx): not found (2763 ms) by blablabla (of 1 avail 1) - CHANNEL (0.8W)
2011/05/04 21:54:29 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2008 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)

If words (pattern) "not found" repeat in each line one after one (like in sample file) and if it reach 10 times, but it is a last 10 line in moment when accessing the log file then DO SOMETHING (I will put my command)

Is there any posibility that this part of code monitoring in real time current file?? I mean if start it once with crond to continuously monitoring my file?

Thx for help!


You can try this:

tail -n 10 -f <filename> | grep -c "not found"

That should give you the count of lines in the last 10 lines of the file that contain not found. You could then set it up to run at timed intervals and do whatever you want if the count is what you are looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消