Awk/Unix group by

前端 未结 6 1485
鱼传尺愫
鱼传尺愫 2020-12-04 14:17

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
         


        
6条回答
  •  孤城傲影
    2020-12-04 15:20

    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
    

提交回复
热议问题