Linux uniq命令

uniq 命令用来检查、删除文本中重复出现的行列,一般与sort命令结合使用。


语法

uniq [-cdu][-f<栏位>][-s<字符位置>][-w<字符位置>][--help][--version][输入文件][输出文件]

参数

-c:在每列旁显示重复出现的次数。

-d:显示重复出现的行列。


实例

原有内容:

[root@localhost ~]# cat file.txt 
test 30  
test 30  
test 30  
Hello 95  
Hello 95  
Hello 95  
Hello 95  
Linux 85  
Linux 85  

删除重复行:(注意:例子中 Linux 85 后面的空格都必须相同,都是3个

[root@localhost ~]# uniq file.txt 
test 30  
Hello 95  
Linux 85  

重复行必须相邻

[root@localhost ~]# cat file1.txt 
test 30  
Hello 95  
Linux 85 
test 30  
Hello 95  
Linux 85 
test 30  
Hello 95  
Linux 85 

结合sort删除重复行:

[root@localhost ~]# sort file1.txt | uniq
Hello 95  
Linux 85 
test 30  

统计各行重复出现的次数:

[root@localhost ~]# sort file1.txt | uniq -c
      3 Hello 95  
      3 Linux 85 
      3 test 30  

显示重复出现的行(列):

[root@localhost ~]# uniq -d file.txt 
test 30  
Hello 95  
Linux 85
原文地址:https://www.cnblogs.com/keye/p/15247168.html