linux 去除重复行 uniq

2024-04-25 09:08
文章标签 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              //显示版本信息并退出  

其中-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就一样了。

这篇关于linux 去除重复行 uniq的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/934249

相关文章

linux生产者,消费者问题

pthread_cond_wait() :用于阻塞当前线程,等待别的线程使用pthread_cond_signal()或pthread_cond_broadcast来唤醒它。 pthread_cond_wait() 必须与pthread_mutex 配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex。当其他线程通过pthread

Linux 安装、配置Tomcat 的HTTPS

Linux 安装 、配置Tomcat的HTTPS 安装Tomcat 这里选择的是 tomcat 10.X ,需要Java 11及更高版本 Binary Distributions ->Core->选择 tar.gz包 下载、上传到内网服务器 /opt 目录tar -xzf 解压将解压的根目录改名为 tomat-10 并移动到 /opt 下, 形成个人习惯的路径 /opt/tomcat-10

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

【Linux进阶】UNIX体系结构分解——操作系统,内核,shell

1.什么是操作系统? 从严格意义上说,可将操作系统定义为一种软件,它控制计算机硬件资源,提供程序运行环境。我们通常将这种软件称为内核(kerel),因为它相对较小,而且位于环境的核心。  从广义上说,操作系统包括了内核和一些其他软件,这些软件使得计算机能够发挥作用,并使计算机具有自己的特生。这里所说的其他软件包括系统实用程序(system utility)、应用程序、shell以及公用函数库等

Windows/macOS/Linux 安装 Redis 和 Redis Desktop Manager 可视化工具

本文所有安装都在macOS High Sierra 10.13.4进行,Windows安装相对容易些,Linux安装与macOS类似,文中会做区分讲解 1. Redis安装 1.下载Redis https://redis.io/download 把下载的源码更名为redis-4.0.9-source,我喜欢跟maven、Tomcat放在一起,就放到/Users/zhan/Documents

Linux系统稳定性的奥秘:探究其背后的机制与哲学

在计算机操作系统的世界里,Linux以其卓越的稳定性和可靠性著称,成为服务器、嵌入式系统乃至个人电脑用户的首选。那么,是什么造就了Linux如此之高的稳定性呢?本文将深入解析Linux系统稳定性的几个关键因素,揭示其背后的技术哲学与实践。 1. 开源协作的力量Linux是一个开源项目,意味着任何人都可以查看、修改和贡献其源代码。这种开放性吸引了全球成千上万的开发者参与到内核的维护与优化中,形成了

Linux 下的Vim命令宝贝

vim 命令详解(转自:https://www.cnblogs.com/usergaojie/p/4583796.html) vi: Visual Interface 可视化接口 vim: VI iMproved VI增强版 全屏编辑器,模式化编辑器 vim模式: 编辑模式(命令模式)输入模式末行模式 模式转换: 编辑-->输入: i: 在当前光标所在字符的前面,转为输入模式

Linux和Mac分卷压缩

使用 zip 命令压缩文件 使用 zip 命令压缩文件,并结合 split 命令来分卷: zip - largefile | split -b 500k 举例: zip - ./tomcat.dmg |split -b 500k 上述命令将文件 largefile 压缩成 zip 包并分卷成不超过 500k 的文件,分解后文件名默认是 x* ,后缀为 2 位a-z 字母,如 aa、ab。

LeetCode--220 存在重复元素 III

题目 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ。 示例 示例 1:输入: nums = [1,2,3,1], k = 3, t = 0输出: true示例 2:输入: nums = [1,0,1,1], k = 1, t = 2输出: true示例

LeetCode--217 存在重复元素

题目 给定一个整数数组,判断是否存在重复元素。如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。 示例 示例 1:输入: [1,2,3,1]输出: true示例 2:输入: [1,2,3,4]输出: false示例 3:输入: [1,1,1,3,3,4,3,2,4,2]输出: true class Solution {p