Grep一个字,并从日志文件找到它的Count不同的时间 [英] 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: "\master1\users\jamesk\Java\chapter05\tech-support-complete\doc\"
Loading source file Error \\master1\users\jamesk\Java\chapter05\tech-support-complete\JamesKohout.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: "\master1\users\jamesk\Java\chapter05\tech-support-complete\doc\"
Loading source file Error \\master1\users\jamesk\Java\chapter05\tech-support-complete\JamesKohout.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: "\master1\users\jamesk\Java\chapter05\tech-support-complete\doc\"
Loading source file Error \\master1\users\jamesk\Java\chapter05\tech-support-complete\JamesKohout.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 "\master1\users\jamesk\Java\chapter05\tech-support-complete\doc\"
Loading source file Error \\master1\users\jamesk\Java\chapter05\tech-support-complete\JamesKohout.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
我想在Unix / python中编写一个脚本,它会覆盖单词Error,并从上面的日志文件中找到它的wordcount在不同的时间。
该文件包含不同时间值的数据。字Error在第一个时间间隔有一个计数6,第二个时间间隔的计数为5,以此类推。
我想输出为
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()。
Here x is your data or file.read().
这篇关于Grep一个字,并从日志文件找到它的Count不同的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!