In a log file with contents as
假设一个log文件名为application.log,且格式为:(实测中发现有可能因为log文件数据不正常,导致统计结果不一致)
2017-11-22 07:29:45.489 INFO com.aaa.bbb.ccc.dubboservice.impl.BingGenTfoTaskServiceImpl [DubboServerHandler-1.2.3.4:18080-thread-199] zzzzAaaAaaaaaa:结束生成aaaaaa,zzzzzz是:56661
2017-11-22 07:29:45.489 ERROR com.aaaaa.aaa.aaa.dubboservice.impl.BingGenTfoTaskServiceImpl [DubboServerHandler-1.2.3.4:18080-thread-199] zzzzAaaAaaaaaa:结束生成bbbbbb,qwerty是:56661
……
执行脚本如下:1
2
3
4
5
6
7
8
9lj@lj-HP-ProBook-640-G1:~/linux-study$ ./getstatic_byhour.sh application.log ERROR
没有收到第三个参数,将搜集所有小时的数据
---all ERROR hava times:3113
2017-11-22 07 have ERROR times:30
2017-11-22 08 have ERROR times:60
......
2017-11-24 15 have ERROR times:10
lj@lj-HP-ProBook-640-G1:~/linux-study$ ./getstatic_byhour.sh application.log ERROR '2017-11-24 09'
2017-11-24 09 have ERROR times:60
实现脚本如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40lj@lj-HP-ProBook-640-G1:~/Downloads/source/linux-study$ cat getstatic_byhour.sh
#!/bin/bash
log_name=""
error_style=""
by_hour=""
function printhelp()
{
echo 'there is no para.invoke sample is:'
echo ''$0' log_folder|log_name error_style [2017-11-24 09]'
echo 'if there is no hour, will display every hour'
}
if [ $# -lt 2 ]; then
printhelp
exit
else
if [ "$1" ]; then
log_name="$1"
fi
if [ "$2" ]; then
error_style="$2"
fi
if [ "$3" ]; then
by_hour="$3"
strr=`grep -caP "^${by_hour}:\d{2}:\d{2}.\d{3} ${error_style}" ${log_name}`
echo "${by_hour} have ${error_style} times:"${strr}
else
echo '没有收到第三个参数,将搜集所有小时的数据'
by_hour=""
strr=`grep -caP "^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3} ${error_style}" ${log_name}`
echo "---all ${error_style} hava times:"${strr}
grep -aP "\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}" ${log_name} | awk -F':' '{print $1}' | uniq | sort > tempgrep.txt
while read line
do
strr=`grep -caP "^${line}:\d{2}:\d{2}.\d{3} ${error_style}" ${log_name}`
echo "${line} have ${error_style} times:"${strr}
done < tempgrep.txt
rm -f ./tempgrep.txt
fi
fi
exit