Linux_Linux_uniq 指令

uniq 在日常工作中也是非常常用的命令,这篇文章来看下 uniq 指令的作用。

注意: uniq 主要是用来做去重,以及计数统计的,但是注意一点 ! uniq 的文件必须预先经过 sort 为有序的文件

 

英文介绍

[root@cdh-manager linux_cmd_test]# uniq --help
Usage: uniq [OPTION]... [INPUT [OUTPUT]]
Filter adjacent matching lines from INPUT (or standard input),
writing to OUTPUT (or standard output).

With no options, matching lines are merged to the first occurrence.

Mandatory arguments to long options are mandatory for short options too.
  -c, --count           prefix lines by the number of occurrences
  -d, --repeated        only print duplicate lines, one for each group
  -D, --all-repeated[=METHOD]  print all duplicate lines
                          groups can be delimited with an empty line
                          METHOD={none(default),prepend,separate}
  -f, --skip-fields=N   avoid comparing the first N fields
      --group[=METHOD]  show all items, separating groups with an empty line
                          METHOD={separate(default),prepend,append,both}
  -i, --ignore-case     ignore differences in case when comparing
  -s, --skip-chars=N    avoid comparing the first N characters
  -u, --unique          only print unique lines
  -z, --zero-terminated  end lines with 0 byte, not newline
  -w, --check-chars=N   compare no more than N characters in lines
      --help     display this help and exit
      --version  output version information and exit

A field is a run of blanks (usually spaces and/or TABs), then non-blank
characters.  Fields are skipped before chars.

Note: 'uniq' does not detect repeated lines unless they are adjacent.
You may want to sort the input first, or use 'sort -u' without 'uniq'.
Also, comparisons honor the rules specified by 'LC_COLLATE'.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'uniq invocation'


中文介绍 

Linux uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。

uniq 可检查文本文件中重复出现的行列。

 

语法

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

相关参数 

  • -c或--count 在每列旁边显示该行重复出现的次数。
  • -d或--repeated 仅显示重复出现的行列。
  • -f<栏位>或--skip-fields=<栏位> 忽略比较指定的栏位。
  • -s<字符位置>或--skip-chars=<字符位置> 忽略比较指定的字符。
  • -u或--unique 仅显示出一次的行列。
  • -w<字符位置>或--check-chars=<字符位置> 指定要比较的字符。
  • --help 显示帮助。
  • --version 显示版本信息。
  • [输入文件] 指定已排序好的文本文件。如果不指定此项,则从标准读取数据;
  • [输出文件] 指定输出的文件。如果不指定此选项,则将内容显示到标准输出设备(显示终端)。

 

案例

构建测试文件 test3.txt  test4.txt

test3.txt

[root@cdh-manager linux_cmd_test]# cat test3.txt
ds
a
sd
a
sd
adc
a
adc
zsw
a
edx
ex

test4.txt

[root@cdh-manager linux_cmd_test]# cat test4.txt
s
ws
ws
sc
sc
wd
wd
wd

 

案例一 基本去重

去重,我们对未排序的文件尝试下去重,test3.txt

[root@cdh-manager linux_cmd_test]# uniq -c test3.txt
      1 ds
      1 a
      1 sd
      1 a
      1 sd
      1 adc
      1 a
      1 adc
      1 zsw
      1 a
      1 edx
      1 ex

可以看到并未进行去重

 

我们对排序后的文件进行去重,test.txt

[root@cdh-manager linux_cmd_test]# uniq test4.txt
s
ws
sc
wd

可以看到完成了去重

 

那么对于 test3.txt 要进行去重,应该怎么办呢? 答案是先用 sort 进行排序,然后进行去重。

[root@cdh-manager linux_cmd_test]# sort test3.txt | uniq 
a
adc
ds
edx
ex
sd
zsw

 

 

 

 

案例二 去重后统计计数

有的时候需要统计每个记录出现的次数,可以使用 -c 选项

[root@cdh-manager linux_cmd_test]# sort test3.txt | uniq -c | sort -r
      4 a
      2 sd
      2 adc
      1 zsw
      1 ex
      1 edx
      1 ds

 

 

 

在很多时候需要对重复内容的统计。可以在使用 -c 的情况下,使用 -d 选项

[root@cdh-manager linux_cmd_test]# sort test3.txt | uniq -d -c
      4 a
      2 adc
      2 sd

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值