“Linux文件的合并、排序和分割” 之 uniq 命令

这个命令非常有用。可以去重 (有效查看log的工具)


功能说明:检查及删除文本文件中重复出现的行列。

语  法:uniq [-cdu][-f<栏位>][-s<字符位置>][-w<字符位置>][--help][--version][输入文件][输出文件]

补充说明:uniq可检查文本文件中重复出现的行列。

参  数:
  -c或--count   在每列旁边显示该行重复出现的次数。
  -d或--repeated   仅显示重复出现的行列。
  -f<栏位>或--skip-fields=<栏位>   忽略比较指定的栏位。
  -s<字符位置>或--skip-chars=<字符位置>   忽略比较指定的字符。
  -u或--unique   仅显示出一次的行列。
  -w<字符位置>或--check-chars=<字符位置>   指定要比较的字符。
  --help   显示帮助。
  --version   显示版本信息。
  [输入文件]   指定已排序好的文本文件。
  [输出文件]   指定输出的文件。

uniq命令的作用:显示唯一的行,对于那些连续重复的行只显示一次!

  接下来通过实践实例说明:

  [root@stu100 ~]# cat test

  boy took bat home

  boy took bat home

  girl took bat home

  dog brought hat home

  dog brought hat home

  dog brought hat home

  看test文件的内容,可以看到其中的连续重复行

  [root@stu100 ~]# uniq test

  boy took bat home

  girl took bat home

  dog brought hat home

  uniq命令不加任何参数,仅显示连续重复的行一次

  [root@stu100 ~]# uniq -c test

  2 boy took bat home

  1 girl took bat home

  3 dog brought hat home

  -c 参数显示文件中每行连续出现的次数。

  [root@stu100 ~]# uniq -d test

  boy took bat home

  dog brought hat home

  -d选项仅显示文件中连续重复出现的行。

  [root@stu100 ~]# uniq -u test

  girl took bat home

  -u选项显示文件中没有连续出现的行。

  [root@stu100 ~]# uniq -f 2 -s 2 test

  boy took bat home

  忽略每行的前2个字段,忽略第二个空白字符和第三个字段的首字符,结果at home

  [root@stu100 ~]# uniq -f 1 test

  boy took bat home

  dog brought hat home

  忽略每行的第一个字段,这样boy ,girl开头的行看起来是连续重复的行


补充:


注意: uniq所认为的重复行是指连续出现的重复行!
我们看日志的时候,可以先用-s或-f跳过日期,再去掉重复行

 

– n 前n个字段与每个字段前的空白一起被忽略。一个字段是一个非空格、非制表符的字符串,彼此由制表符和空格隔开(字段从0开始编号)。

+n 前n个字符被忽略,之前的字符被跳过(字符从0开始编号)。

– f n 与- n相同,这里n是字段数。

– s n 与+n相同,这里n是字符数。


实例详解

uniq.txt的原文如下:

[root@umail39 tmp]#cat uniq.txt
this is a test
this is a test
This is a test
i love you
i love you
we are good
this is a pen
i do not know
this is a pen

1.在每行前加上表示相应行目出现次数,但只会检查相邻重复的行。

[root@umail39 tmp]#uniq -c uniq.txt
      2 this is a test
      1 This is a test
      2 i love you
      1 we are good
      1 this is a pen
      1 i do not know
      1 this is a pen

2.,-f 1 忽略了第一列,检查重复从第二字段开始的,检查的时候,不区分大小写.

[root@umail39 tmp]#uniq -f 1 -c uniq.txt
      3 this is a test
      2 i love you
      1 we are good
      1 this is a pen
      1 i do not know
      1 this is a pen

3.检查的时候,不考虑前4个字符

[root@umail39 tmp]#uniq -s 4 -c uniq.txt
      3 this is a test
      2 i love you
      1 we are good
      1 this is a pen
      1 i do not know
      1 this is a pen

4.去重复的项,然后全部显示出来

[root@umail39 tmp]#uniq  -u uniq.txt
This is a test
we are good
this is a pen
i do not know
this is a pen

5.对每行第2个字符以后的内容不作检查

[root@umail39 tmp]#uniq -w 2 -c uniq.txt  
      2 this is a test
      1 This is a test
      2 i love you
      1 we are good
      1 this is a pen
      1 i do not know
      1 this is a pen



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值