In a log file with contents as [ERROR_NO] [MESSAGE]- Human readable text display summary/count of specific error numbers that occurred every hour or a specific hour.

假设一个 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
......

执行脚本如下:

lj@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

实现脚本如下:

#8194;lj@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


↙↙↙阅读原文可查看相关链接,并与作者交流