跟“shell脚本--awk数组入门案例”这篇文章一样的做法,唯一不同的是处理多个文件,其实直接在awk的后面直接加多个文件即可。
测试文件
[root@172-0-10-222 myscripts]# cat testfile
a 2
b 4
c 1
d 5
[root@172-0-10-222 myscripts]# cat testfile2
b 2
c 2
a 4
d 1
e 3
awk数组处理多个文件中字母出现的次数:
[root@172-0-10-222 myscripts]# awk '{arr[$1]+=$2}END{for(i in arr){print i,arr[i]}}' testfile testfile2
a 6
b 6
c 3
d 6
e 3