Wednesday, January 9, 2008

Maximum occurrence - using AWK


The 'cabdetails_week2_Mar_1998.out" file contains the details of employees of "Ramu Generators Ltd" who used the cab services for a particular week.
Ramu, the owner wants to see who took this cab facility maximum number of times

$ cat cabdetails_week2_Mar_1998.out
Jay Kumar|ID2340|12|Processing
Hari G|ID2342|11|Finance
Jay Kumar|ID2340|19|Processing
Anil S|ID2341|34|Marketing
Hari G|ID2342|15|Finance
Lenin|ID2349|12|Processing
Inayat|ID2367|12|Report
...
...

$ awk '{
arr[$1]++
if ( arr[$1] >= max )
max=arr[$1]
}
END {
for ( name in arr )
if ( max == arr[name] )
print name"("max " times"")"
}' FS="|" cabdetails_week2_Mar_1998.out

The output:
Jay Kumar(2 times)
Hari G(2 times)

No comments:

© Jadu Saikia www.UNIXCL.com