Awk command print max min values from log

220 views Asked by At

I am doing grep with success flag S. $11 is the service type, $12 is either S(success) or F(failure), and $14 is elapsed time. Below command I am getting average elapsed time.

bash-3.2$ grep 'EXSTAT|' ivrbroker.log | grep '|F|' |
> /usr/xpg4/bin/awk -F"|" '{a[$11]++;c[$11]+=$14}
>    END{for(b in a){print b"," a[b]","c[b]/a[b]}}'
QCPE,2,276.5
bash-3.2$ grep 'EXSTAT|' ivrbroker.log|grep '|F|'       
EXSTAT|IVR|2014|11|17|14|43|57|1086|SRQCPE952|QCPE|F|100|349
EXSTAT|IVR|2014|11|17|15|35|51|1092|SRQCPE741|QCPE|F|100|204

But now I am looking for max elapsed time and min elapsed time like below.

QCPE,2,276.5,349,204

bash-3.2$ grep '|F|' ivrbroker.log
EXSTAT|IVR|2014|11|17|14|43|57|1086|SRQCPE952|QCPE|F|100|349
EXSTAT|IVR|2014|11|17|15|35|51|1092|SRQCPE741|QCPE|F|100|204
bash-3.2$ awk -F'|' 'BEGIN { OFS="," }
    /EXSTAT\|/ && /\|F\|/ { a[$11]++; c[$11] += $14;
        if (a[$11]==1) { max[$11]=$14; min[$11]=$14; }
        if($14 > max[$11]) max[$11]=$14;  if($14 < min[$11]) min[$11]=$14; }
    END { for(b in a) print b, a[b], c[b]/a[b], max[b], min[b] }' ivrbroker.log
,204,2,0,349
bash-3.2$ /usr/xpg4/bin/awk -F'|' 'BEGIN { OFS="," }                                                                                                                                                     
    /EXSTAT\|/ && /\|F\|/ { a[$11]++; c[$11] += $14;
       if (a[$11]==1) { max[$11]=$14; min[$11]=$14; }
       if($14 > max[$11]) max[$11]=$14;  if($14 < min[$11]) min[$11]=$14; }
    END { for(b in a) print b, a[b], c[b]/a[b], max[b], min[b] }' ivrbroker.log
,204,2,276.5,349
bash-3.2$ 
1

There are 1 answers

2
tripleee On

You need to add two more array variables to keep track of the min and max.

While you are at it, get rid of the grep | awk antipattern.

awk -F'|' 'BEGIN { OFS="," }
    /EXSTAT\|/ && /\|F\|/ { a[$11]++; c[$11] += $14;
        if (!($11 in max)) max[$11]=min[$11]=$14;
        if($14 > max[$11]) max[$11]=$14;  if($14 < min[$11]) min[$11]=$14; }
    END { for(b in a) print b, a[b], c[b]/a[b], max[b], min[b] }' ivrbroker.log

Looks like maybe the first condition should be $1 == "EXSTAT" && $12 == "F" for slightly improved legibility and precision. Maybe also rename a to count and c to sum.

The above script works on Linux, but apparently not on SunOS / SysV / XPG4 Awk. Perhaps try this minor modification:

awk -F'|' 'BEGIN { OFS="," }
    /EXSTAT\|/ && /\|F\|/ { a[$11]++; c[$11] += $14;
        if (a[$11]==1) { max[$11]=$14; min[$11]=$14; }
        if($14 > max[$11]) max[$11]=$14;  if($14 < min[$11]) min[$11]=$14; }
    END { for(b in a) print b, a[b], c[b]/a[b], max[b], min[b] }' ivrbroker.log