001、按照原始顺序输出
[root@pc1 test02]# ls a.txt [root@pc1 test02]# cat a.txt ## 测试数据 a 76 b 78 a 100 c 222 b 7777 b 8888 a 9999 b 3333 d 1111 e 7777777 e 9999999 f yyyyyy f 777777 g 733333 f 333333 f 999999 f gggggg f wwwwww f 666666 a ppppp e ggggg e fffff ## 输出第一列重复4次的数据, 按照原始顺序输出 [root@pc1 test02]# awk '{OFS = "\t"; print ++ay[$1], $0}' a.txt | tac | awk '++ay[$2] == 1 && $1 == 4' | awk '{print $2}' | awk '{ay[NR] = $0} END {for(i = 1; i <= NR; i++) {print NR, ay[i]}}' | cat - a.txt | awk '{if(NR == 1) {a = $1}; if(NR <= a) {ay[$2]} else if($1 in ay) {print $0}}' a 76 b 78 a 100 b 7777 b 8888 a 9999 b 3333 e 7777777 e 9999999 a ppppp e ggggg e fffff
002、排序后输出
[root@pc1 test02]# ls a.txt [root@pc1 test02]# cat a.txt a 76 b 78 a 100 c 222 b 7777 b 8888 a 9999 b 3333 d 1111 e 7777777 e 9999999 f yyyyyy f 777777 g 733333 f 333333 f 999999 f gggggg f wwwwww f 666666 a ppppp e ggggg e fffff [root@pc1 test02]# cut -f 1 a.txt | sort | uniq -c | awk '$1 == 4 {print $2}' > tmp.txt [root@pc1 test02]# cat tmp.txt a.txt | awk -v a=$(wc -l < tmp.txt) '{if(NR <= a) {ay[$0]} else if($1 in ay) {print $0}}' a 76 b 78 a 100 b 7777 b 8888 a 9999 b 3333 e 7777777 e 9999999 a ppppp e ggggg e fffff
.
标签:输出,pc1,次数,awk,linux,test02,txt,root From: https://www.cnblogs.com/liujiaxin2018/p/17783938.html