awk 数值做统计

awk 数值做统计
[root@asmboy Ex_6.127-6.146]# more datafile4
4234  Tom    43
4567  Arch   45
2008  Eliza  65
4571  Tom    22
3298  Eliza  21
4622  Tom    53
2345  Mary   24
[root@asmboy Ex_6.127-6.146]# awk '{count[$2]++}END{for(name in count)print name,count[name]}' datafile4
Arch 1
Tom 3
Eliza 2
Mary 1
 
[root@asmboy Ex_6.127-6.146]# more employees
Tom Jones       4424    5/12/66 54335
Mary Adams      5346    11/4/63 28765
Sally Chang     1654    7/22/54 65000
Billy Black     1683    9/23/44 33650
[root@asmboy Ex_6.127-6.146]# awk '{name[x++]=$2};END{for(i=0;i<NR;i++)print i,name[i]}' employees
0 Jones
1 Adams
2 Chang
3 Black
 
[root@asmboy Ex_6.127-6.146]# more datafile3
tom
mary
sean
tom
mary
mary
bob
mary
alex
[root@asmboy Ex_6.127-6.146]# awk '/tom/{count["tom"]++}END{print count["tom"]}' datafile3
2
 
[root@asmboy Ex_6.127-6.146]# more db
Tom Jones
Mary Adams
Sally Chang
Billy Black
Tom Savage
Tom Chung
Reggie Steel
Tommy Tucker
[root@asmboy Ex_6.127-6.146]# awk '/^Tom/{name[NR]=$1};END{for(i=1;i<= NR;i++)print name[i]}' db
Tom
 
Tom
Tom
Tommy
[root@asmboy Ex_6.127-6.146]# awk '/^Tom/{name[NR]=$1};END{for(i in name)print name[i]}' db
Tom
Tom
Tommy
Tom
 
 

你可能感兴趣的:(shell,职场,休闲)