Tuesday, January 16, 2007

AWK: Report Generation


summary.txt contains the details of the employee's working hours.

$ cat summary.txt
A|Jan|clerk|02:45
B|Jan|Salesman|02:12
C|Jan|Accountant|03:12
A|Feb|clerk|01:10
B|Feb|Salesman|11:10
B|March|Salesman|3:10
C|Feb|Accountant|3:34

The output is required in a report format, like this

A = 235 mins

B = 992 mins
C = 406 mins

or

A = 3:55
B = 16:32
C = 6:46

This is how it can be achieved:

#First format
$ awk 'BEGIN{FS="[|,:]"} {arr[$1]+=$(NF-1)*60+$NF} END {for (i in arr) {print i,"=",arr[i],"mins"}}' summary.txt

#Second format
$ awk 'BEGIN{FS="[|,:]"} {arr[$1]+=$(NF-1)*60+$NF} END {for (i in arr) {printf "%c = %d:%d\n",i,arr[i]/60,arr[i]%60}}' summary.txt

Points to digest:
1) FS="[|,:]" - how to specify multiple FS in AWK
2) NF=number of fields in a line, $NF prints the last field.
3) Use of associative array in AWK.

No comments:

© Jadu Saikia www.UNIXCL.com