uniq命令全称是“unique”,中文释义是“独特的,唯一的”。该命令的作用是用来去除文本文件中连续的重复行,中间不能夹杂其他文本行。去除了重复的,保留的都是唯一的,也就是独特的,唯一的了。
我们应当注意的是,它和sort的区别,sort只要有重复行,它就去除,而uniq重复行必须要连续,也可以用它忽略文件中的重复行。
语法格式:uniq [参数] [文件]
常用参数:
-c | 打印每行在文本中重复出现的次数 |
-d | 只显示有重复的纪录,每个重复纪录只出现一次 |
-u | 只显示没有重复的纪录 |
参考实例
删除连续文件中连续的重复行:
1 |
[root@ledlinux ~]# cat testfile <br> test 30 <br> test 30 <br> test 30 <br> Hello 95 <br> Hello 95 <br> Hello 95 <br> Hello 95 <br> Linux 85<br> Linux 85<br> [root@ledlinux ~]# uniq testfile <br> test 30 <br> Hello 95 <br> Linux 85 |
打印每行在文件中出现重复的次数:
1 |
[root@ledlinux ~]# uniq -c testfile <br> 3 test 30 <br> 4 Hello 95 <br> 2 Linux 85 |
只显示有重复的纪录,且每个纪录只出现一次:
1 |
[root@ledlinux ~]# uniq -d testfile <br> test 30 <br> Hello 95 <br> Linux 85 |
只显示没有重复的纪录:
1 |
[root@ledlinux ~]# uniq -u testfile <br>[root@ledlinux ~]# |