如何在Linux中刪除已排序檔案中重複的行?


要刪除已排序檔案中重複的行並使其唯一,我們使用Linux系統中的uniq命令。uniq命令作為一種過濾器程式,報告檔案中重複的行。它過濾掉輸入中相鄰的匹配行,並提供唯一的輸出。此命令也可用於Windows和IBM i作業系統。

語法

uniq命令的通用語法如下所示

uniq [OPTION]... [INPUT [OUTPUT]]

fmt命令中可用選項的簡要說明。

序號選項及說明
1-c, --count
顯示每行重複的次數。
2-d—repeated
僅顯示重複的行,每組一行。
3-D
顯示所有重複的行。
4-f, --skip-fields=N
避免比較前N個欄位。
5-i, --ignore-case
比較時忽略大小寫差異。
6-s, --skip-chars=N
避免比較前N個字元。
7-u, --unique
僅列印唯一行
8-w, --check-chars=N
行分隔符為NULL,而不是換行符
9-v, --verbose
比較行中不超過N個字元。
10--help
顯示幫助資訊並退出。
11--version
輸出版本資訊並退出。

要列印刪除檔案中重複行後的內容,我們使用Linux系統中的uniq命令,如下所示。

$ cat >text.txt
Print only unique lines.
The earth is round.
The earth is round.
Welcome to the tutorialpoint...
Welcome to the tutorialspint...
$ uniq text.txt
Print only unique lines.
The earth is round.
Welcome to the tutorialpoint...

要列印檔案的重複行數,我們使用-c或--count選項以及uniq命令,如下所示。

$ uniq –c text.txt
2 The earth is round.
2 Welcome to the tutorialspoint...
1 Print only unique lines.

要僅列印檔案的唯一行,我們使用-u或–unique選項以及uniq命令,如下所示。

$ uniq –u text.txt
Print only unique lines.

要檢查有關uniq命令的更多資訊,我們使用--help選項以及Linux作業系統中的uniq命令,如下所示。

$ uniq --help

要檢查uniq命令的版本資訊,我們使用--version選項以及Linux作業系統中的uniq命令,如下所示。

$ uniq --version

更新於:2021年7月1日

7K+ 瀏覽量

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.