

新闻资讯
技术学院Linux 中的 uniq 命令主要用于识别和移除文本文件中重复的行,通常会配合 sort 命令一起使用。
uniq 可以用来查找文本文件中重复出现的行内容。
uniq [-cdu][-f][-s][-w][--help][--version][输入文件][输出文件]
参数说明:
假设在 testfile 文件中,第 2、3、5、6、7、9 行为相同内容,可以使用以下命令来去重:
uniq testfile
testfile 的原始内容如下:
$ cat testfile #原始内容 test 30 test 30 test 30 Hello 95 Hello 95 Hello 95 Hello 95 Linux 85 Linux 85
运行 uniq 命令后,输出结果为:
$ uniq testfile #去重后的结果 test 30 Hello 95 Linux 85
若想查看每一行重复的次数,并在行前显示该次数,可使用以下命令:
uniq -c testfile
输出如下:
$ uniq -c testfile #显示重复次数 3 test 30 #表示该行出现了3次 4 Hello 95 #表示该行出现了4次 2 Linux 85 #表示该行出现了2次
需要注意的是,如果重复的行不是连续的,uniq 命令将无法正确识别,例如下面这种情况:
$ cat testfile1 #原始内容 test 30 Hello 95 Linux 85 test 30 Hello 95 Linux 85 test 30 Hello 95 Linux 85
此时应先使用 sort 命令排序,再结合 uniq 命令处理:
$ sort testfile1 | uniq Hello 95 Linux 85 test 30
统计各行在文件中的出现频率:
$ sort testfile1 | uniq -c 3 Hello 95 3 Linux 85 3 test 30
查找文件中重复的行内容:
$ sort testfile1 | uniq -d Hello 95 Linux 85 test 30