简体   繁体   中英

How to create histogram using bash with the custom log structure

I need some help in getting the bash histogram of the log information generated from the server. The log structure is like this

HH:MM:SS <column 1>,<column 2>,<topic name>,<Start_Time>,<End_Time>

I want to extract the HH:MM:SS and <topic name> and the difference in time and show

16:12:33 topic_1 02 hrs 30 mins 00 sec ----------------------------->
16:12:34 topic_2 01 hrs 00 mins 00 sec ------------->
16:12:35 topic_3 00 hrs 02 mins 00 sec ->
16:12:36 topic_4 00 hrs 30 mins 00 sec ------>
16:12:37 topic_5 00 hrs 15 mins 00 sec --->
16:12:38 topic_6 00 hrs 20 mins 00 sec ---->
16:12:39 topic_7 00 hrs 25 mins 00 sec ----->
16:12:40 topic_8 00 hrs 02 mins 00 sec ->
...
...
...

I tried searching and could not come up with any proper way to cut and get this done efficiently.

This is where I reached

grep <search_term> log_file | cut -f2,16 -d' ' | awk -F, '{print $5-$4 "," $0}' | awk -F, '{print $1  " " $4}'

Can any one help?

Ignoring DST, leap seconds, leap days, etc.:

$ cat file
01:02:03 <column 1>,<column 2>,topic_1,01:02:03,01:12:10
01:02:03 <column 1>,<column 2>,topic_2,01:02:03,01:18:20

.

$ cat tst.awk
BEGIN { FS="," }
{
    split($(NF-1),b,/:/)
    split($NF,e,/:/)
    begSecs = 60*60*b[1] + 60*b[2] + b[3]
    endSecs = 60*60*e[1] + 60*e[2] + e[3]

    durSecs = endSecs - begSecs
    durMins = int(durSecs/60)
    durHrs  = int(durMins/60)

    if      ( t == "h" ) { dur = durHrs  }
    else if ( t == "m" ) { dur = durMins }
    else                 { dur = durSecs }

    dashes = sprintf("%*s",dur,"") ">"
    gsub(/ /,"-",dashes)

    sub(/ .*/,"",$1)
    print $1, $3, durHrs, "hrs", durMins-60*durHrs, "mins", durSecs-60*durMins, "secs\t" dashes
}

.

$ awk -f tst.awk file
01:02:03 topic_1 0 hrs 10 mins 7 secs  ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------->
01:02:03 topic_2 0 hrs 16 mins 17 secs ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------->

.

$ awk -v t='m' -f tst.awk file
01:02:03 topic_1 0 hrs 10 mins 7 secs  ---------->
01:02:03 topic_2 0 hrs 16 mins 17 secs ---------------->

You didn't say if each - should represent a second, a minute, an hour, or something else so I showed how to modify that by setting t .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM