uniq 指令可以作用去除重複資料,對象可以是檔案或者 pipe 管線輸入。
uniq 去除重複資料時,只會對連續重複的行進行過濾處理,所以很多時 uniq 會配合 sort 指令一同使用,或者用 “sort -u” 實現。
uniq 使用例子:
例如有一個檔案,裡面的內容如下:
This is a testing.
This is a testing.
This is a testing.
This is also a testing.
This is also a testing.
This is line 3.
執行以下指令過濾重複內容:
$ uniq filename.txt
輸出結果是:
This is a testing.
This is also a testing.
This is line 3.
如果上面的資料,每行重複的資料不是連續出現,可以先用 sort 進行排序,再用 uniq 過濾重覆的部份,具體指令是:
$ sort filename.txt | uniq
以上執行 uniq 只會將結果直接輸出,如果想將以上結果匯入到檔案,可以用 pipe 做:
$ uniq filename >> new_filename.txt