【Linux】一步一步学Linux——uniq命令(54)
发布日期:2021-06-29 20:59:50 浏览次数:2 分类:技术文章

本文共 1880 字,大约阅读时间需要 6 分钟。

00. 目录

文章目录

01. 命令概述

uniq - 删除排序文件中的重复行

uniq命令用于报告或忽略文件中的重复行,一般与sort命令结合使用。

uniq命令全称是“unique”,中文释义是“独特的,唯一的”。该命令的作用是用来去除文本文件中连续的重复行,中间不能夹杂其他文本行。去除了重复的,保留的都是唯一的,也就是独特的,唯一的了。

我们应当注意的是,它和sort的区别,sort只要有重复行,它就去除,而uniq重复行必须要连续,也可以用它忽略文件中的重复行。

02. 命令格式

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

03. 常用选项

从输入文件或者标准输入中筛选相邻的匹配行并写入到输出文件或标准输出。不附加任何选项时匹配行将在首次出现处被合并。长选项必须使用的参数对于短选项时也是必需使用的。  -c, --count           在每行前加上表示相应行目出现次数的前缀编号  -d, --repeated        只输出重复的行  -D, --all-repeated[=delimit-method    显示所有重复的行                        delimit-method={
none(default),prepend,separate} 以空行为界限 -f, --skip-fields=N 比较时跳过前N 列 -i, --ignore-case 在比较的时候不区分大小写 -s, --skip-chars=N 比较时跳过前N 个字符 -u, --unique 只显示唯一的行 -z, --zero-terminated 使用'\0'作为行结束符,而不是新换行 -w, --check-chars=N 对每行第N 个字符以后的内容不作对照 --help 显示此帮助信息并退出 --version 显示版本信息并退出若域中为先空字符(通常包括空格以及制表符),然后非空字符,域中字符前的空字符将被跳过。提示:uniq 不会检查重复的行,除非它们是相邻的行。如果您想先对输入排序,使用没有uniq 的"sort -u"。同时,比较服从"LC_COLLATE" 变量所指定的规则。

04. 参考示例

4.1 忽略文件中的重复行

[deng@localhost test]$ cat txt111111112222222211112222333344445555[deng@localhost test]$ uniq  txt1111222211112222333344445555[deng@localhost test]$

4.2 统计各行在文件中出现的次数

[deng@localhost test]$ sort txt | uniq -c       3 1111      3 2222      1 3333      1 4444      1 5555[deng@localhost test]$

4.3 只输出重复的行

[deng@localhost test]$ sort txt | uniq -d11112222[deng@localhost test]$

4.4 显示所有重复的行

[deng@localhost test]$ sort txt | uniq -D111111111111222222222222[deng@localhost test]$

4.5 只显示唯一的行

[deng@localhost test]$ sort txt | uniq -u333344445555[deng@localhost test]$

4.6 只比较前三个字符

[deng@localhost test]$ cat txt111111112222222211122222333344445555[deng@localhost test]$ sort txt | uniq  -w 311112222333344445555[deng@localhost test]$

4.7 使用-d -c统计重复行出现的次数

[deng@localhost test]$ sort txt | uniq -cd       2 1111      3 2222[deng@localhost test]$

05. 附录

参考:

转载地址:https://dengjin.blog.csdn.net/article/details/96504079 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【Linux】一步一步学Linux——tr命令(55)
下一篇:【Linux】一步一步学Linux——sort命令(53)

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月12日 00时09分04秒