【Linux】一步一步学Linux——uniq命令(54)

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 txt
1111
1111
2222
2222
1111
2222
3333
4444
5555
[deng@localhost test]$ uniq  txt
1111
2222
1111
2222
3333
4444
5555
[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 -d
1111
2222
[deng@localhost test]$ 

4.4 显示所有重复的行

[deng@localhost test]$ sort txt | uniq -D
1111
1111
1111
2222
2222
2222
[deng@localhost test]$ 

4.5 只显示唯一的行

[deng@localhost test]$ sort txt | uniq -u
3333
4444
5555
[deng@localhost test]$ 

4.6 只比较前三个字符

[deng@localhost test]$ cat txt
1111
1111
2222
2222
1112
2222
3333
4444
5555
[deng@localhost test]$ sort txt | uniq  -w 3
1111
2222
3333
4444
5555
[deng@localhost test]$ 

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

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

05. 附录

参考:【Linux】一步一步学Linux系列教程汇总

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值