Alex Rivera | Logout

Awk/Unix group by

Asked 2013-02-17T00:40:31.640
48

have this text file:

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

Trying to get this (count):

joe 1
jim 1
bob 2
mike 3

Thanks,

Edit
Report

1 Answer

31

Strip the header row, drop the age field, group the same names together (sort), count identical runs, output in desired format.

tail -n +2 txt.txt | cut -d',' -f 1 | sort | uniq -c | awk '{ print $2, $1 }'

output

bob 2
jim 1
joe 1
mike 3
answered 2013-02-17T00:44:27.650

Your Answer