实例详细说明linux下去除重复行命令uniq

一、uniq干什么用的

linux下去除文本重复行的命令之一。从网上看来两个注意事项:

1,对文本操作时,它一般会和sort命令进行组合使用,因为uniq 不会检查重复的行,除非它们是相邻的行。如果您想先对输入排序,使用sort -u。

2,对文本操作时,若域中为先空字符(通常包括空格以及制表符),然后非空字符,域中字符前的空字符将被跳过

二、uniq参数说明

用法:uniq [选项]... [文件] 
从输入文件或者标准输入中筛选相邻的匹配行并写入到输出文件或标准输出。 
 
不附加任何选项时匹配行将在首次出现处被合并。 
 
长选项必须使用的参数对于短选项时也是必需使用的。 
 -c, --count              //在每行前加上表示相应行目出现次数的前缀编号 
 -d, --repeated          //只输出重复的行 
 -D, --all-repeated      //只输出重复的行,不过有几行输出几行 
 -f, --skip-fields=N     //-f 忽略的段数,-f 1 忽略第一段 
 -i, --ignore-case       //不区分大小写 
 -s, --skip-chars=N      //根-f有点像,不过-s是忽略,后面多少个字符 -s 5就忽略后面5个字符 
 -u, --unique            //去除重复的后,全部显示出来,根mysql的distinct功能上有点像 
 -z, --zero-terminated   end lines with 0 byte, not newline 
 -w, --check-chars=N      //对每行第N 个字符以后的内容不作对照 
 --help              //显示此帮助信息并退出 
 --version              //显示版本信息并退出

 

 

三,测试文本文件uniqtest

  1. this is a test  
  2. this is a test  
  3. this is a test  
  4. i am tank  
  5. i love tank  
  6. i love tank  
  7. this is a test  
  8. whom have a try  
  9. WhoM have a try  
  10. you  have a try  
  11. i want to abroad  
  12. those are good men  
  13. we are good men  

四,实例详解

  1. [zhangy@BlackGhost mytest]$ uniq -c uniqtest  
  2.  3 this is a test  
  3.  1 i am tank  
  4.  2 i love tank  
  5.  1 this is a test           //和第一行是重复的   
  6.  1 whom have a try  
  7.  1 WhoM have a try  
  8.  1 you  have a try  
  9.  1 i want to abroad  
  10.  1 those are good men  
  11.  1 we are good men  

从上例子中我们可以看出,uniq的一个特性,检查重复行的时候,只会检查相邻的行。重复数据,肯定有很多不是相邻在一起的。

  1. [zhangy@BlackGhost mytest]$ sort uniqtest |uniq -c  
  2.  1 WhoM have a try  
  3.  1 i am tank  
  4.  2 i love tank  
  5.  1 i want to abroad  
  6.  4 this is a test  
  7.  1 those are good men  
  8.  1 we are good men  
  9.  1 whom have a try  
  10.  1 you  have a try  

这样就可以解决上个例子中提到的问题

  1. [zhangy@BlackGhost mytest]$ uniq -d -c uniqtest  
  2.  3 this is a test  
  3.  2 i love tank  

uniq -d 只显示重复的行

  1. [zhangy@BlackGhost mytest]$ uniq -D uniqtest  
  2. this is a test  
  3. this is a test  
  4. this is a test  
  5. i love tank  
  6. i love tank  

uniq -D 只显示重复的行,并且把重复几行都显示出来。他不能和-c一起使用

  1. [zhangy@BlackGhost mytest]$ uniq -f 1 -c uniqtest  
  2.  3 this is a test  
  3.  1 i am tank  
  4.  2 i love tank  
  5.  1 this is a test  
  6.  2 whom have a try  
  7.  1 you  have a try  
  8.  1 i want to abroad  
  9.  2 those are good men   //只有一行,显示二行   

在这里those只有一行,显示的却是重复了,这是因为,-f 1 忽略了第一列,检查重复从第二字段开始的。

  1. [zhangy@BlackGhost mytest]$ uniq -i -c uniqtest  
  2.  3 this is a test  
  3.  1 i am tank  
  4.  2 i love tank  
  5.  1 this is a test  
  6.  2 whom have a try  //一个大写,一个小写   
  7.  1 you  have a try  
  8.  1 i want to abroad  
  9.  1 those are good men  
  10.  1 we are good men  

检查的时候,不区分大小写

  1. [zhangy@BlackGhost mytest]$ uniq -s 4 -c uniqtest  
  2. 3 this is a test  
  3. 1 i am tank  
  4. 2 i love tank  
  5. 1 this is a test  
  6. 3 whom have a try   //根上一个例子有什么不同   
  7. 1 i want to abroad  
  8. 1 those are good men  
  9. 1 we are good men  

检查的时候,不考虑前4个字符,这样whom have a try 就和 you have a try 就一样了。

  1. [zhangy@BlackGhost mytest]$ uniq -u uniqtest  
  2. i am tank  
  3. this is a test  
  4. whom have a try  
  5. WhoM have a try  
  6. you  have a try  
  7. i want to abroad  
  8. those are good men  
  9. we are good men  

去重复的项,然后全部显示出来

  1. [zhangy@BlackGhost mytest]$ uniq -w 2 -c uniqtest  
  2.  3 this is a test  
  3.  3 i am tank  
  4.  1 this is a test  
  5.  1 whom have a try  
  6.  1 WhoM have a try  
  7.  1 you  have a try  
  8.  1 i want to abroad  
  9.  1 those are good men  
  10.  1 we are good men  

对每行第2个字符以后的内容不作检查,所以i am tank 根 i love tank就一样了。

 

转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/shell/1022.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值