zl程序教程

您现在的位置是:首页 >  工具

当前栏目

uniq命令学习

2023-09-14 09:11:20 时间

转自:https://www.runoob.com/linux/linux-comm-uniq.html

1.介绍

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

uniq 可检查文本文件中重复出现的行列。

  • -c或--count 在每列旁边显示该行重复出现的次数。
  • -d或--repeated 仅显示重复出现的行列。
  • -u或--unique 仅显示出一次的行列。

2.例子

testfile文件内容为:

$ cat testfile      #原有内容  
test 30  
test 30  
test 30  
Hello 95  
Hello 95  
Hello 95  
Hello 95  
Linux 85  
Linux 85 

统计:

$ uniq -c testfile      #删除重复行后的内容  
3 test 30             #前面的数字的意义为该行共出现了3次  
4 Hello 95            #前面的数字的意义为该行共出现了4次  
2 Linux 85            #前面的数字的意义为该行共出现了2次 

当重复的行并不相邻时,uniq 命令是不起作用的,可以结合sort来:

$ sort testfile1 | uniq -c
   3 Hello 95  
   3 Linux 85 
   3 test 30