AWK - Is it possible to Breakdown a log file by a distinct field && by hour

≯℡__Kan透↙ 提交于 2019-12-02 07:41:37

You'll want something like:

$ cat tst.awk
BEGIN { FS="[ :]" }
{ sum[$6,$2]+=$7; msgs[$6]; hrs[$2] }
END {
    for (msg in msgs) {
        print msg
        for (hr in hrs) {
            print hr, sum[msg,hr]+0
        }
        print ""
    }
}

$ awk -f tst.awk file
Message1
00 12
01 13

Message2
00 50
01 10

but obviously it's a bit of a guess since it's run against your posted sample input but you didn't provide the associated expected output.

btw wrt the question subject line AWK - Is it possible..., assuming it's about manipulating text the answer to that question is always "yes" so no need to ask if it's possible.

I just noticed your previous question where you say the hour may not always be present in your data so this may be what you really are looking for:

$ cat tst.awk
BEGIN { FS="[ :]" }
{ sum[$6,$2+0]+=$7; msgs[$6] }
END {
    for (msg in msgs) {
        print msg
        #for (hr=0; hr<=23; hr++) {
        for (hr=0; hr<=4; hr++) {
            printf "%02d %d\n", hr, sum[msg,hr]
        }
        print ""
    }
}
$
$ awk -f tst.awk file
Message1
00 12
01 13
02 0
03 0
04 0

Message2
00 50
01 10
02 0
03 0
04 0

Change the "4" to "23" obviously. I'd also recommend you consider a CSV output instead so you can import to Excel, etc., e.g.:

$ cat tst.awk
BEGIN { FS="[ :]"; OFS="," }
{ sum[$6,$2+0]+=$7; msgs[$6] }
END {
    printf "hr"
    for (msg in msgs) {
        printf "%s%s", OFS, msg
    }
    print ""
    for (hr=0; hr<=4; hr++) {
        printf "%02d", hr
        for (msg in msgs) {
            printf "%s%d", OFS, sum[msg,hr]
        }
        print ""
    }
}

$ awk -f tst.awk file
hr,Message1,Message2
00,12,50
01,13,10
02,0,0
03,0,0
04,0,0

$ awk -f tst.awk file | column -s, -t
hr  Message1  Message2
00  12        50
01  13        10
02  0         0
03  0         0
04  0         0
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!