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