刷新一个单词并从不同时间的日志文件中找到它的计数
包含以下数据的日志文件。
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
我想在Unix / python中编写一个脚本,用它来查询单词“Error”,并在不同的时间从上面的日志文件中找到它的单词计数。 该文件包含不同时间间隔的数据。单词错误在第一个时间间隔有一个计数6,第二个时间间隔的计数是5,依此类推。 我想输出为
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
我试着用下面的命令但它只给出整个文件中的总字数。
grep -i "Error" | wc -l
请帮忙。 谢谢。
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
直接awk
:
awk '/^201[0-9].*:/{if (cont){print cont}cont=0;printf $0}/Error/{cont+=1}END{print cont}' infile
解释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/78069.html
上一篇: Grep a word and find its Count from log file for different times
下一篇: Shell script that finds a word within a file selected by a user