一、命令简介

  Linux uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。uniq 命令读取由 InFile 参数指定的标准输入或文件,uniq可检查文本文件中重复出现的行列。

二、命令示例

0、示例文件

[root@test1 tmp]# cat test.txt
hello, wuhs is a man
hello, wuhs is a man
Hello, wuhs is a man
hello, sunru is a woman
hello, sunru is a woman
hello, sunru is a woman
hello, bulesky is a children
hi, bulesky is a children

1、剔除重复的行

[root@test1 tmp]# uniq test.txt
hello, wuhs is a man
Hello, wuhs is a man
hello, sunru is a woman
hello, bulesky is a children
hi, bulesky is a children

2、显示未重复的行

[root@test1 tmp]# uniq -u test.txt
Hello, wuhs is a man
hello, bulesky is a children
hi, bulesky is a children

3、统计重复行的数量

[root@test1 tmp]# uniq -c test.txt
2 hello, wuhs is a man
1 Hello, wuhs is a man
3 hello, sunru is a woman
1 hello, bulesky is a children
1 hi, bulesky is a children

4、忽略大小写统计重复行

[root@test1 tmp]# uniq -ci test.txt
3 hello, wuhs is a man
3 hello, sunru is a woman
1 hello, bulesky is a children
1 hi, bulesky is a children

5、忽略比较第1个字符

[root@test1 tmp]# uniq -s 1 test.txt
hello, wuhs is a man
hello, sunru is a woman
hello, bulesky is a children
hi, bulesky is a children

6、显示重复出现的行

[root@test1 tmp]# uniq -d test.txt
hello, wuhs is a man
hello, sunru is a woman

7、忽略比较第一段(空格为分段分隔符)

[root@test1 tmp]# uniq -f 1 -c test.txt
3 hello, wuhs is a man
3 hello, sunru is a woman
2 hello, bulesky is a children

三、使用语法及参数说明

1、使用语法

用法:uniq [OPTION]… [INPUT [OUTPUT]]

2、参数说明

参数参数说明
-c或–count在每列旁边显示该行重复出现的次数。
-d或–repeated仅显示重复出现的行列。
-D, --all-repeated[=METHOD]打印所有重复行
组可以用空行分隔
方法={none(默认),prepend,separate}
-f<栏位>或–skip-fields=<栏位>忽略比较指定的栏位。
-i, --ignore-case比较时忽略大小写差异
-s<字符位置>或–skip-chars=<字符位置>忽略比较指定的字符。
-u或–unique仅显示出一次的行列。
-z, --zero-terminated以0字节结束行,而不是换行
-w<字符位置>或–check-chars=<字符位置>指定要比较的字符。
–help显示帮助。
–version显示版本信息。
[输入文件]指定已排序好的文本文件。如果不指定此项,则从标准读取数据;
[输出文件]指定输出的文件。如果不指定此选项,则将内容显示到标准输出设备(显示终端)。
Logo

更多推荐