Grep a word and find its Count from log file for different times
log file which contains following data.
2014-10-19 17:30:25:
Creating destination directory: "master1usersjameskJavachapter05tech-support-completedoc"
Loading source file Error master1usersjameskJavachapter05tech-support-completeJamesKohout.java...
onstructing Javadoc information...Error
31 Error Standard Doclet version 1.6.0_26 Error
-encoding Error
19 windows-1252
20 -charset Error
21 windows-1252
22 -docletpath
2014-10-19 18:30:25:
Creating destination directory: "master1usersjameskJavachapter05tech-support-completedoc"
Loading source file Error master1usersjameskJavachapter05tech-support-completeJamesKohout.java...
onstructing Javadoc Error information...
31 Standard Doclet version 1.6.0_26 Error
-encoding Error
19 windows-1252
20 -charset Error
21 windows-1252
22 -docletpath
2014-10-19 19:30:25:
Creating destination directory: "master1usersjameskJavachapter05tech-support-completedoc"
Loading source file Error master1usersjameskJavachapter05tech-support-completeJamesKohout.java...
onstructing Javadoc information...Error
31 Standard Doclet version 1.6.0_26 Error
-encoding
19 windows-1252
20 -charset Error
21 windows-1252
22 -docletpath
2014-10-19 20:30:25:
Creating destination directory:Error "master1usersjameskJavachapter05tech-support-completedoc"
Loading source file Error master1usersjameskJavachapter05tech-support-completeJamesKohout.java...
onstructing Javadoc information...
31 Standard Doclet version 1.6.0_26 Error
-encoding Error
19 windows-1252
20 -charset Error
21 windows-1252 Error
22 Error -docletpath
I want to write a script in Unix/python which greps the word "Error" and finds the wordcount of it from above log file at different times. The file contains Data of Different time intevals.The word Error has a count 6 at the first time interval,the count at second time interval is 5 and so on. I want the output as
2014-10-19 17:30:25: Error Count=6
2014-10-19 18:30:25: Error Count=5
2014-10-19 19:30:25: Error Count=4
2014-10-19 20:30:25: Error Count=7
I tried with the following command But it only gives the total word count present in the whole file.
grep -i "Error" | wc -l
Please Help. Thanks.
import re
pattern=re.compile(r"d{4}-d{1,2}-d{1,2}s+d{1,2}:d{1,2}:d{1,2}:|Error",re.IGNORECASE)
ll=pattern.findall(x)
d={}
for x in ll:
if x!="Error":
d[x]=0
last=x
else:
d[last]=d[last]+1
print d
这里x是你的数据或file.read()。
Awk简单的工作。
awk '/^[0-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9] [012][0-9]:[0-5][0-9]:[0-6][0-9]:/ {
t=$0 }
/Error/ { ++e[t] }
END { for (s in e) print s "Error-Count=" e[s] }' logfile
A direct awk
:
awk '/^201[0-9].*:/{if (cont){print cont}cont=0;printf $0}/Error/{cont+=1}END{print cont}' infile
Explained code
:
awk '/^201[0-9].*:/{ # Timestamp pattern reached
if (cont){
print cont # print previus timestamp
} # counter if exists and not zero
cont=0 # initialize actual timestamp counter
printf $0
} # print timestamp WITHOUT linebreak
/Error/{ # Error patter reached
cont+=1 # Aaccumulated count
}
END{
print cont # print remainder counter
}' infile
链接地址: http://www.djcxy.com/p/78070.html