一,uniq干什么用的

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

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

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

二,uniq参数说明

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

其中-z不知道有什么用

三,测试文本文件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就一样了。

 

uniq linux下去除重复行命令的更多相关文章

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

    地址:http://blog.51yip.com/shell/1022.html 一,uniq干什么用的 文本中的重复行,基本上不是我们所要的,所以就要去除掉.linux下有其他命令可以去除重复行,但 ...

  2. linux 下删除重复行-- uniq 与 awk

    $ cat file liw liw liw hdsui mdksjd liw $ cat file | uniq -u # 只删除相邻的,不保留重复行 hdsui mdksjd liw $ cat ...

  3. Linux合并文件、去除重复行的命令

    Linux合并文件命令: awk '{printf("%s\n",$0)}' YQ-*101?.txt >  123.txt   linux去除重复行命令:cat YQ-10 ...

  4. uniq 去除重复行

    1.命令功能 uniq可以输出或忽略文件中的重复行,经常需要使用sort先对文件进行排序,然后使用uniq去重并计数. 2.语法格式 uniq  option  input uniq   选项    ...

  5. linux shell脚本查找重复行/查找非重复行/去除重复行/重复行统计

    转自:http://blog.sina.com.cn/s/blog_6797a6700101pdm7.html 去除重复行 sort file |uniq 查找非重复行 sort file |uniq ...

  6. linux下使用无线网卡的命令行方法(wifi,iwconfig)

    原文地址:linux下使用无线网卡的命令行方法(wifi,iwconfig) 作者:andyhzw (1)首先关闭开发板的有线网卡 [root@FriendlyARM /]# ifconfig eth ...

  7. Linux下一款可以使用命令行的pdf阅读器

    Zathura是linux下一款用命令行控制打pdf阅读器,并且基本打使用方法和vim很相似.对于喜欢键盘操作的用户来说的确是一个不错的选择. ubuntu下的安装命令: sudo apt-get i ...

  8. linux下如何使用sftp命令【转】

    linux下如何使用sftp命令 from:   http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888391.html sftp 是一个 ...

  9. linux下mysql操作的命令

    最近在学习mysql,还是只菜鸟,找到下面篇文章对初学者挺有用的,所以共享下 1.linux下启动mysql的命令:   mysqladmin start /ect/init.d/mysql star ...

随机推荐

  1. 基于bootstrap3的 表格和分页的插件

    如题 样式呢就是bootstrap3 的 功能呢就是实现表格和分页 (以上废话) 本来是自己没事儿写的一个js插件,曾经搁浅了一阵子,但最近由于公司项目的原因也需要这样的一个插件,所以就捡起来做了个可 ...

  2. WPF中实现根据拼音查找汉字

    1.WPF的一个触摸屏项目,需要输入姓名,但是屏幕不支持汉字输入,使用虚拟键盘不稳定,为了解决该问题特此进行处理. 2.新建一个类转换类,里面初始化一个数组,数组包含拼音,以及拼音下的常用的汉字. 3 ...

  3. Mac 平台下安装 OpenVC

    opencv for Mac I 背景 Mac(Ios 9.0), 时间: 2015年11月.       本人也是小白,在网上爬了几天才安装好. II 安装OpenCV for Mac 首先下载op ...

  4. tar解压去除文件夹

    tar zxvf test.tar.gz  --strip-components 1 解压到当前目录,并去除一级目录

  5. PHPCMS 核心代码与 www 分离部署

    为了满足更多用户二次开发的兴趣与爱好,同时,为了更加安全.可以通过修改入口代码的包含方式来让主程序和www程序分开. 先看下面目录结构: (图1) 我们需要将 phpcms 目录和 index.php ...

  6. mysql 查看数据库大小

    select table_schema, concat(truncate(sum(data_length)/1024/1024,2),' mb') as data_size,concat(trunca ...

  7. 【pyhton】【转】修改递归次数

    import sys sys.setrecursionlimit(1500) # set the maximum depth as 1500 def recursion(n): if(n <= ...

  8. cx_Oracle ORA-24315: 非法的属性类型的解决办法

    网上查找原因说: 出现该错误的原因是因为版本不兼容. 检查了一下python版本和oracle 的版本,我的oracle client是10g的,python的版本是2.7, 但我安装的cx_orac ...

  9. stream的Read、Write方法实例

    , bytes.Length)) > ) , readBytes-);//8为偏移量,10为数量                    }                }            ...

  10. ubuntu下安装ssh

    vm安装ubunt后,无法连接ssh,一般情况是防火墙的问题,或者SSH没有安装. 防火墙 1.查看防火墙 sudo ufw status 2,关闭防火墙 sudo ufw disable ssh 1 ...