Published: 22 May 2018 › Updated: 22 May 2018

AWK常见用法(二)
AWK实例演示
当出现合并文件的需求时,可以考虑使用数组
- 合并两个文件,a1和a2:
# cat a1
1 苹果 水果
2 梨子 水果
3 草莓 水果
4 土豆 蔬菜
5 恐龙 动物
# cat a2
01,苹果 水果
02,梨子 水果
03,香蕉 水果
04,土豆 蔬菜
05,香肠 食品
# awk -F"[ ,]" 'NR==FNR{a[$2]=$1}NR!=FNR{print $0,a[$2]}' a1 a2
01,苹果 1
02,梨子 2
03,香蕉
04,土豆 4
05,香肠
# cat b1
tom:1000
bob:1001
mary:1002
# cat b2
1000:a.txt
1000:b.txt
1001:c.txt
1001:d.txt
1002:e.txt
1002:f.txt
# awk -F: 'NR==FNR{a[$2]=$0}NR!=FNR{print a[$1]":"$2}' b1 b2
tom:1000:a.txt
tom:1000:b.txt
bob:1001:c.txt
bob:1001:d.txt
mary:1002:e.txt
mary:1002:f.txt
通过这两个例子,我们可以看出,在使用数组的时候尽量已相同的部分作为数组的下标
- 将相同行合并为一行
# cat a1
1 苹果
1 水果
2 梨子
2 水果
3 草莓
3 水果
4 土豆
4 蔬菜
5 恐龙
5 动物
# awk 'NR==1{printf "%s",$0;x=$1}NR!=1{if($1==x){printf " %s",$2}else printf "\n%s",$0;x=$1}END{printf "\n"}' a1
1 苹果 水果
2 梨子 水果
3 草莓 水果
4 土豆 蔬菜
5 恐龙 动物
- 求交集
# cat a1
10/11111111,11/22222222,12/33333333,13/44444444
14/55555555,15/66666666,16/77777777,17/88888888
# cat a2
11111111
66666666
56567778
98987654
# awk -F[/,] 'NR==FNR{a[$1]}NR!=FNR{if($2 in a){print $0}}' a2 a1
10/11111111,11/22222222,12/33333333,13/44444444
- 统计次数
# cat a1
tom tom bob mary
zhangsan lisi
bob mary tom
# awk '{sum+=NF}END{print sum}' a1
9
# awk '{for(i=1;i<=NF;i++){a[$i]++}}END{for(x in a){print x"出现了"a[x]"次"}}' a1
mary出现了2次
zhangsan出现了1次
tom出现了3次
lisi出现了1次
bob出现了2次
- 求和
# cat /proc/1/smaps | awk '/Shared_Clean/{AA+=$2}/Shared_Dirty/{BB+=$2}END{print "干净页:"AA"KB";print "脏页:"BB"KB"}'
干净页:2672KB
脏页:0KB
Leave AWK常见用法(二) to:
Read more #linux posts
Best Posts From pmliang
We have not curated any of pmliang's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.