linux: uniq

一,uniq干什么用的

文本中的重复行,基本上不是我们所要的,所以就要去除掉。linux下有其他命令可以去除重复行,但是我觉得uniq还是比较方便的一个。使用uniq的时候要注意以下二点

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

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

二,uniq参数说明

[zhangy@BlackGhost ~]$ uniq --help
用法: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

this is a test
this is a test
this is a test
i am tank
i love tank
i love tank
this is a test
whom have a try
WhoM have a try
you  have a try
i want to abroad
those are good men
we are good men

四,实例详解

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

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

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

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

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

uniq -d 只显示重复的行

[zhangy@BlackGhost mytest]$ uniq -D uniqtest
this is a test
this is a test
this is a test
i love tank
i love tank

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

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

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

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

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

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

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

[zhangy@BlackGhost mytest]$ uniq -u uniqtest
i am tank
this is a test
whom have a try
WhoM have a try
you  have a try
i want to abroad
those are good men
we are good men

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

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

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 正则表达式 关于正则表达式相信很多学计算机的人都听说过 尤其是做编程行业的人 那什么是正则表达式 正则表达式,又称...
    数据革命阅读 979评论 0 1
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 13,034评论 2 33
  • 基础命令 主要的命令和快捷键 Linux系统命令由三部分组成:cmd + [options]+[operation...
    485b1aca799e阅读 1,193评论 0 0
  • 第1章 小试牛刀 $ 是普通用户,# 表示管理员用户 root。 shebang:#!。sharp / hash ...
    巴喬書摘阅读 6,625评论 1 4
  • 一. 常用命令 文件目录操作 cd 切换目录cd dirname ls 显示文件/目录属性 pwd 显示当前路径p...
    羽非衣阅读 2,867评论 0 2

友情链接更多精彩内容