Sunday, 15 January 2012

Awk/Unix group by -



Awk/Unix group by -

have text file:

name, age joe,42 jim,20 bob,15 mike,24 mike,15 mike,54 bob,21

trying (count):

joe 1 jim 1 bob 2 mike 3

thanks,

$ awk -f, 'nr>1{arr[$1]++}end{for (a in arr) print a, arr[a]}' file.txt joe 1 jim 1 mike 3 bob 2 explanations -f, splits on , nr>1 treat lines after line 1 arr[$1]++ increment array arr (split ,) first column key end{} block executed @ end of processing file for (a in arr) iterating on arr a key print a print key , arr[a] , value a of array arr

unix awk

No comments:

Post a Comment