uniq 与sort是难兄难弟,有uniq常常需要sort命令,这是因为当重复的行并不相邻时,uniq 命令是不起作用的,即若文件内容为以下时,uniq 命令不起作用:
$ cat testfile1 # 原有内容 test 30 Hello 95 Linux 85 test 30 Hello 95 Linux 85 test 30 Hello 95 Linux 85
这时我们就可以使用 sort:
$ sort testfile1 | uniq Hello 95 Linux 85 test 30
当文件中重复行不相邻时,uniq命令无法直接去除重复。通过结合使用`sort`先对文件排序,然后用`uniq`可以有效过滤重复内容。例如,对于包含多组重复行的`testfile1`,使用`sort testfile1 | uniq`将得到去重后的输出:Hello95, Linux85, test30。
1057

被折叠的 条评论
为什么被折叠?



