How to count with grep or awk, pattern if it occurred x times respectively (one after the other)
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)开发者_JAVA百科 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/04 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, 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?
EDIT Now I see that I need something more in script than if else.. How to implement to my script watching all time (realtime) last 10 line from file?
Thx for help!
something like
#!/bin/bash
while ${cleanLogFile:-true} ; do
if (( $(grep -c "not found" logFile) > 9 )) ; then
echo "Too many 'not found's in logFile"
# do your stuff here
cleanLogFile=false
fi
done
Given your 0% acceptance rate on help you have recieved from others, I am reluctant to invest spending time until I know you are willing to 'pay' for it. Please realize that accepting and voting for help you receive does not come out of your reputation total.
I hope this helps.
This AWK code prints out the message if and only if there are 10 or more consecutive lines with "not found":
! /not found/ {
if (count >= 10) {
print "More than 10 lines found"
}
count = 0
}
/not found/ {
++count
}
END {
if (count >= 10) {
print "More than 10 lines found"
}
精彩评论