awk '{sum[$2]+=1}END{for(i in sum)print i"\t"sum[i]}' test.txt 如文件test.txt第2列的值为"00""01"或"02",执行结果如下: [root@localhost cc]# cat test.txt a 00 b 01 c 00 d 02 [root@localhost cc]# awk '{sum[$2]+=1}END{for(i in sum)print i"\t"sum[i]}' test.txt 00 2 01 1 02 1 [root@localhost cc]#
6、如只统计文件test.txt中第2列"00"或"01"出现的次数,命令可写为
[root@localhost cc]# awk '{if($2=="00") ++sum1;if($2=="01") ++sum2}END{print "00""\t"sum1"\n""01""\t"sum2}' test.txt 00 2 01 1
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx