linux之去重uniq命令详解

linux之去重uniq命令详解

uniq是对文本文件进行行去去重的工具。

  • 以行为单位,进行行与行之间的字符串比较并进行去重
  • 只能对有序的文本行进行有效去重,所以常与sort命令结合使用
参数解释
-c统计行出现的次数
-d只显示重复的行并且去重
-u只显示唯一的行
-i忽略字母大小写
-f忽略前N个字段(字段间用空白字符分隔)
1.与sort结合使用

准备文件

[root@linuxforliuhj test]# cat hello.sh 
hello this is linux
be better
be better
i am lhj
hello this is linux
i am lhj
i am lhj
be better
i am lhj
have a nice day
have a nice day
hello this is linux
hello this is linux
have a nice day
zzzzzzzzzzzzzz
dddddddd
gggggggggggggggggggg
[root@linuxforliuhj test]# 

【1】单独使用uniq命令

[root@linuxforliuhj test]# uniq hello.sh
hello this is linux
be better
i am lhj
hello this is linux
i am lhj
be better
i am lhj
have a nice day
hello this is linux
have a nice day
zzzzzzzzzzzzzz
dddddddd
gggggggggggggggggggg
[root@linuxforliuhj test]#

可以看出单独使用uniq命令时只对相邻重复行进行去重,无法进行有效去重

【2】与sort结合使用

[root@linuxforliuhj test]# sort hello.sh | uniq
be better
dddddddd
gggggggggggggggggggg
have a nice day
hello this is linux
i am lhj
zzzzzzzzzzzzzz
[root@linuxforliuhj test]# 

先排序使重复的行相邻,然后使用uniq可以有效去重

【3】uniq支持管道符

[root@linuxforliuhj test]# cat hello.sh | sort | uniq
be better
dddddddd
gggggggggggggggggggg
have a nice day
hello this is linux
i am lhj
zzzzzzzzzzzzzz
[root@linuxforliuhj test]# 

上次写sort那篇文章时说到sort支持管道符,同样uniq也一样支持管道符

2.统计出现的次数,使用-c参数

使用-c,–count可以统计重复行出现的次数

[root@linuxforliuhj test]# cat hello.sh | sort | uniq -c
      3 be better
      1 dddddddd
      1 gggggggggggggggggggg
      3 have a nice day
      4 hello this is linux
      4 i am lhj
      1 zzzzzzzzzzzzzz
[root@linuxforliuhj test]#
3.只显示重复的行并且去重,使用-d参数

使用-d,–repeated只显示重复的行并且重复的行只显示一次

[root@linuxforliuhj test]# cat hello.sh | sort | uniq -dc
      3 be better
      3 have a nice day
      4 hello this is linux
      4 i am lhj
[root@linuxforliuhj test]# 

可以看到末尾的三行不重复的行没有显示,将重复的行进行去重后显示

3.只显示唯一的行,使用-u参数
[root@linuxforliuhj test]# cat hello.sh | sort | uniq -u
dddddddd
gggggggggggggggggggg
zzzzzzzzzzzzzz
[root@linuxforliuhj test]# 

只有末尾三行是唯一的,所以只显示末尾的三行文本

4.忽略字母大小写,使用-i参数
[root@linuxforliuhj test]# cat test.txt 
I am LHJ
i am lhj
HELLO this Is Zhang
hello this is zhang
[root@linuxforliuhj test]# cat test.txt | sort | uniq -i
hello this is zhang
i am lhj
[root@linuxforliuhj test]#
5.忽略前N个字段进行去重,使用-f参数
[root@linuxforliuhj test]# cat test.txt 
1 I am LHJ
2 i am lhj
3 HELLO this Is Zhang
4 hello this is zhang
[root@linuxforliuhj test]# cat test.txt | sort | uniq -i -f 1
1 I am LHJ
3 HELLO this Is Zhang
[root@linuxforliuhj test]# 

test.txt文件中每一行之前有行号,无法使用sort和uniq进行去重,可以使用-f参数忽略每一行的第一个字段,这样就可以忽略每一行之前的行号,对每一行之后的内容进行去重处理。

注意:字段之间必须是空白字符(空格或者tap键均属于空白字符)分隔,使用其他字符无法识别
对于这种情况也可以使用awk命令工具进行处理去除第一列的行号,然后通过管道符丢给sort和uniq命令处理,后续会继续更新awk等重要的文本处理工具。

  • 9
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Operose-honeybee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值