awk数组计算某一列重复出现次数
测试文件
[root@172-0-10-222 myscripts]# cat testfile
andy 15871731153
hanna 18787876543
hello 15578765389
welcome 15578765389
andy 13987273647
hello 15871731153
andy 15871731153
统计每个手机号码出现的次数
[root@172-0-10-222 myscripts]# cat testfile | awk '{arr[$2]++}END{for(i in arr){print i,arr[i]}}'
18787876543 1
15871731153 3
13987273647 1
15578765389 2
按照出现次数从大到小排序
[root@172-0-10-222 myscripts]# cat testfile | awk '{arr[$2]++}END{for(i in arr){print i,arr[i]}}' | sort -r -k 2
15871731153 3
15578765389 2
18787876543 1
13987273647 1
注意:sort默认按照第一列的从小到大排序,-r表示从大到小排序,-k表示根据第几列排。
案例:统计文件中不同网站域名的出现次数
测试文件
[root@172-0-10-222 myscripts]# cat testfile
http://www.baidu.com
http: