點擊上方「民工哥技術之路」,選擇「設為星標」
回復「1024」獲取獨家整理的學習資料!
昨日推薦:每天學一個 Linux 命令(32):sort
uniq 命令用於去除文件中重複行,一般與 sort 命令結合使用。
語法格式uniq [選項] [標準輸入 [輸出]]
uniq [OPTION] [INPUT [OUTPUT]]輸入文件 #指定要去除的重複行文件。如果不指定該項,則從標準讀入
輸出文件 #指定要去除重複行後的內容要寫入的輸出文件。如果不指定此項,則將內容顯示到標準輸出設備(顯示終端)。
選項說明-c #在每列旁邊顯示該行重複出現的次數
-d #只顯示重複出現的行與列
-f #忽略比較指定的欄位
-s #忽略比較指定的字符
-i #不區分大小寫的比較
-u #只顯示出現過一次的行與列
-w #指定要比較的字符
-z #用0位元組(NULL)代替換行符
--help #顯示幫助信息並退出
--version #顯示版本信息並退出
應用舉例#刪除重複行
[root@centos7 ~]# cat test.txt
This is a test line
This is a test line
This is a test line
This is also a test line
This is also a test line
This is also also a test line
[root@centos7 ~]# uniq test.txt
This is a test line
This is also a test line
This is also also a test line
[root@centos7 ~]# sort test.txt | uniq
This is also also a test line
This is also a test line
This is a test line
#只顯示單一行
[root@centos7 ~]# uniq -u test.txt
This is also also a test line
[root@centos7 ~]# sort test.txt |uniq -u
This is also also a test line
#統計各行在文件中出現的次數
[root@centos7 ~]# sort test.txt |uniq -c
1 This is also also a test line
2 This is also a test line
3 This is a test line
#在文件中找出重複的行
[root@centos7 ~]# sort test.txt |uniq -d
This is also a test line
This is a test line