linux命令uniq去重

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

一,uniq干什么用的

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

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

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

二,uniq参数说明

1 [zhangy@BlackGhost ~]$ uniq --help

2 用法:uniq [选项]... [文件]

3 从输入文件或者标准输入中筛选相邻的匹配行并写入到输出文件或标准输出。

4

5 不附加任何选项时匹配行将在首次出现处被合并。

6

7 长选项必须使用的参数对于短选项时也是必需使用的。

8  -c, --count              //在每行前加上表示相应行目出现次数的前缀编号

9  -d, --repeated          //只输出重复的行

10  -D, --all-repeated      //只输出重复的行,不过有几行输出几行

11  -f, --skip-fields=N     //-f 忽略的段数,-f 1 忽略第一段

12  -i, --ignore-case       //不区分大小写

13  -s, --skip-chars=N      //根-f有点像,不过-s是忽略,后面多少个字符 -s 5就忽略后面5个字符

14  -u, --unique            //去除重复的后,全部显示出来,根mysql的distinct功能上有点像

15  -z, --zero-terminated   end lines with 0 byte, not newline

16  -w, --check-chars=N      //对每行第N 个字符以后的内容不作对照

17  --help              //显示此帮助信息并退出

18  --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

四,实例详解

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

[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

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

[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

uniq -d 只显示重复的行

[zhangy@BlackGhost mytest]$ uniq -d -c uniqtest

3 this is a test

2 i love tank

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

[zhangy@BlackGhost mytest]$ uniq -D uniqtest

this is a test

this is a test

this is a test

i love tank

i love tank

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

[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   //只有一行,显示二行

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

[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

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

[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

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

[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

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

[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

linux 命令 uniq的更多相关文章

  1. Linux 命令 - uniq: 通知或忽略重复行

    给定一个已排好序的文件,uniq 会删除重复行并将结果输出到标准输出中.uniq 通常与 sort 结合使用以删除 sort 输出内容中的重复行. 命令格式 uniq [OPTION]... [INP ...

  2. 实例解说Linux命令行uniq

    Linux命令uniq的作用是过滤重复部分显示文件内容,这个命令读取输入文件,并比较相邻的行.在正常情况下,第二个及以后更多个重复行将被删去,行比较是根据所用字符集的排序序列进行的.该命令加工后的结果 ...

  3. linux之uniq

    Linux命令uniq的作用是过滤重复部分显示文件内容,这个命令读取输入文件,并比较相邻的行.在正常情况下,第二个及以后更多个重复行将被删去,行 比较是根据所用字符集的排序序列进行的.该命令加工后的结 ...

  4. linux sort,uniq,cut,wc命令详解

    linux sort,uniq,cut,wc命令详解 sort sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sort 命令将这些 ...

  5. linux命令(12)uniq去重

    转载地址:http://blog.51yip.com/shell/1022.html 实例详细说明linux下去除重复行命令uniq 一,uniq干什么用的 文本中的重复行,基本上不是我们所要的,所以 ...

  6. (转)linux sort,uniq,cut,wc命令详解

    linux sort,uniq,cut,wc命令详解 sort sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sort 命令将这些 ...

  7. 【转帖】linux sort,uniq,cut,wc,tr,xargs命令详解

    linux sort,uniq,cut,wc,tr,xargs命令详解 http://embeddedlinux.org.cn/emb-linux/entry-level/201607/21-5550 ...

  8. Linux日常之命令uniq

    命令uniq 作用是过滤文件内容重复部分 需要注意的是,该命令只是对相邻的行进行比较,若两个相同的行不相邻,不会被过滤掉 选项 -c,在每行行首显示出该行出现的次数 -d,只显示出重复的行 -u,只显 ...

  9. Linux 命令之head, tail, tr, sort, uniq, grep

    head [filename] head -n 11 [filename] -> First 11 lines head -c 20 [filename] -> First 20 char ...

随机推荐

  1. spring cloud Eureka client配置(consumer通过Eureka发起对provider的调用)

    参考:http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html springboot版本:2.0.3 ...

  2. sql随机时间

    declare @endtime datetime declare @starttime datetime set @starttime='2017-09-01' set @endtime = '20 ...

  3. .net core系列之《.net core中使用集成IDistributedCache接口的Redis和MongoDB实现分布式缓存》

    分布式的缓存可以提高性能和可伸缩性的 ASP.NET Core 应用程序,尤其是托管在云中或服务器场中时. 什么是分布式的缓存 分布式的缓存由多个应用程序服务器共享,缓存中的信息不存储在单独的 Web ...

  4. CAGradientLayer渐变颜色动画

    CAGradientLayer渐变颜色动画 或许你用过CAGradientLayer,你知道他是用于渐变颜色的,但你是否直到,CAGradientLayer的渐变颜色是可以动画的哦. 源码: // / ...

  5. windows 下 gdb 的安装

    在 windows 下 gcc/g++ 的安装 这篇文章中已经提到,用MinGW Installation Manager可以方便地管理 MinGW 组件,因此使用该软件安装 gdb . 打开 Min ...

  6. 内置模块之os

    os模块主要对操作系统进行调用的接口 一.os模块常用方法和属性 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("d:") 改变 ...

  7. January 08 2017 Week 2nd Sunday

    Believe not all that you see nor half what you hear. 眼见的不能全信,耳闻的也不能半信. What you hear, what you see, ...

  8. 51nod 1102 面积最大的矩形

    题目地址在这儿 求取:以某矩形g[i]为最小值的区间的左右端点,得到一个临时解.所有临时解中的最大值即为解. 求取区间的方法可以用单调栈,也可以用下面这种十分简洁的类似于递归的方法.下面这种解法求出来 ...

  9. centos6.4 minimal 安装kvm

    操作系统是网易源下载的centos 64位的minimal安装包,很多工具都没有,像gcc make wget which where 等统统没有,好在有yum 这里为了简单起见直接用yum安装kvm ...

  10. 左右值无限级分类 MVC + EntityFramework 的简单实现

    在度娘上查了大半个月的资料,最后发现每个网友分享的文章都有一定的错误(PS:大家是故意的么?).最后是在看了一个ASP版本后知道了大概流程:看了一个存储过程实现的文章后知道了大概需要的功能:看了一个S ...