linux 中统计相同序列出现的次数

 

001、

(base) [root@PC1 test]# ls
a.txt
(base) [root@PC1 test]# cat a.txt     ## 测试数据
1
2
3
1
2
3
1
2
3                        ## 将相同的序列转换为行
(base) [root@PC1 test]# awk '{if(NR % 3 == 0) {print $0} else {printf("%s ", $0)}}' a.txt
1 2 3
1 2 3
1 2 3                    ## 检测是否为相同的序列
(base) [root@PC1 test]# awk '{if(NR % 3 == 0) {print $0} else {printf("%s ", $0)}}' a.txt | uniq
1 2 3                    ## 统计相同序列的次数
(base) [root@PC1 test]# awk '{if(NR % 3 == 0) {print $0} else {printf("%s ", $0)}}' a.txt | uniq -c
      3 1 2 3

 

 

002、

(base) [root@PC1 test]# ls
a.txt  b.txt
(base) [root@PC1 test]# cat b.txt   ## 测试数据b.txt
1
2
3
1
2
3
1
2
3
1
2
3
1
2
3                       ## 重复五次
(base) [root@PC1 test]# awk '{if(NR % 3 == 0) {print $0} else {printf("%s ", $0)}}' b.txt | uniq -c
      5 1 2 3

 

posted @ 2022-09-22 11:57  小鲨鱼2018  阅读(151)  评论(0编辑  收藏  举报