uniq命令可以将重复行从输出文件中删除
语法:uniq 【选项】【文件】
选项说明:
-c:显示输出中,每行行首加上本行在文件中出现的次数
-d:只显示重复行
-u:只显示文件中不重复的行
-n:前n个字段和每个字段前的空白一起被忽略
+n:前n个字符被忽略
假设现在有个文件file2位于/home/test目录下:
[root@localhost test]# cat file2
hj
anhui
hefei
shanhai
saghiputong
shanghai
anhui
shanghai
shanghai
beijing
beijing
[root@localhost test]# uniq file2 file3//删除了重复行,但必须要求连续两行或者多行是重复的才可以删除
[root@localhost test]# cat file3
hj
anhui
hefei
shanhai
saghiputong
shanghai
anhui
shanghai
beijing
[root@localhost test]# uniq -c file2 file4//命令选项c的使用
[root@localhost test]# cat file4
1 hj
1 anhui
1 hefei
1 shanhai
1 saghiputong
1 shanghai
1 anhui
2 shanghai
2 beijing
[root@localhost test]# uniq -d file2 file5//命令选项d的使用
[root@localhost test]# cat file5
shanghai
beijing
[root@localhost test]# uniq -u file2 file6//命令选项u的使用
[root@localhost test]# cat file6
hj
anhui
hefei
shanhai
saghiputong
shanghai
anhui