← 返回列表

Linux shell uniq 命令

Linux shell uniq 命令用于检查及删除文本文件中重复出现的行列。 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 显示版本信息 [输入文件] 指定已排序好的文本文件 [输出文件] 指定输出的文件 范例 文件 demo.txt 中第2 行、第5 行、第9 行为相同的行,使用 uniq 命令删除重复的行 root@localhost ~]# cat demo.txt test 30 test 30 test 30 Hello 95 Hello 95 Hello 95 Hello 95 Linux 85 Linux 85 使用 uniq 命令删除重复的行后,有如下输出结果: [root@localhost ~]# uniq demo.txt test 30 Hello 95 Linux 85 Linux 85 检查文件并删除文件中重复出现的行,并在行首显示该行重复出现的次数 [root@localhost ~]# uniq -c demo.txt 3 test 30 4 Hello 95 1 Linux 85 1 Linux 85 返回上一级

Linux shell uniq 命令用于检查及删除文本文件中重复出现的行列。

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 显示版本信息 [输入文件]

指定已排序好的文本文件

[输出文件] 指定输出的文件

范例

文件 demo.txt 中第2 行、第5 行、第9 行为相同的行,使用 uniq 命令删除重复的行

root@localhost ~]# cat demo.txt
test 30
test 30
test 30
Hello 95
Hello 95
Hello 95
Hello 95
Linux 85
Linux 85

使用 uniq 命令删除重复的行后,有如下输出结果:

[root@localhost ~]# uniq demo.txt
test 30
Hello 95
Linux 85
Linux 85

检查文件并删除文件中重复出现的行,并在行首显示该行重复出现的次数

[root@localhost ~]# uniq -c demo.txt
      3 test 30
      4 Hello 95
      1 Linux 85
      1 Linux 85

返回上一级

打开资源 ↗

交流与评论