Awk/Unix group by

前端 未结 6 1473
鱼传尺愫
鱼传尺愫 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:09

    A strictly awk solution...

    BEGIN { FS = "," }
    { ++x[$1] }
    END { for(i in x) print i, x[i] }
    

    If name, age is really in the file, you could adjust the awk program to ignore it...

    BEGIN   { FS = "," }
    /[0-9]/ { ++x[$1] }
    END     { for(i in x) print i, x[i] }
    

提交回复
热议问题