linux uniq命令詳解

2021-09-02 04:31:03 字數 2457 閱讀 7542

**:

uniq [選項] 檔案

說明:這個命令讀取輸入檔案,並比較相鄰的行。在正常情況下,第二個及以後更多個重複行將被刪去,行比較是根據所用字符集的排序序列進行的。該命令加工後的結果寫到輸出檔案中。輸入檔案和輸出檔案必須不同。如果輸入檔案用「- 」表示,則從標準輸入讀取。

該命令各選項含義如下:、

– c 顯示輸出中,在每行行首加上本行在檔案中出現的次數。它可取代- u和- d選項。

– d 只顯示重複行。

– u 只顯示檔案中不重複的各行。

– n 前n個字段與每個欄位前的空白一起被忽略。乙個欄位是乙個非空格、非製表符的字串,彼此由製表符和空格隔開(欄位從0開始編號)。

+n 前n個字元被忽略,之前的字元被跳過(字元從0開始編號)。

– f n 與- n相同,這裡n是字段數。

– s n 與+n相同,這裡n是字元數。

接下來通過實踐例項說明:

****************************************===

[root@stu100 ~]# cat test

boy took bat home

boy took bat home

girl took bat home

boy took bat home

boy took bat home

dog brought hat home

dog brought hat home

dog brought hat home

看test檔案的內容

****************************************====

[root@stu100 ~]# uniq test

boy took bat home

girl took bat home

boy took bat home

dog brought hat home

uniq命令不加任何引數,僅顯示連續重複的行一次

****************************************====

[root@stu100 ~]# uniq -c test

2 boy took bat home

1 girl took bat home

2 boy took bat home

3 dog brought hat home

1

-c 引數顯示檔案中每行連續出現的次數。

****************************************====

[root@stu100 ~]# cat test |sort | uniq -c

14 boy took bat home

3 dog brought hat home

1 girl took bat home

排序後再顯示

****************************************====

[root@stu100 ~]# uniq -d test

boy took bat home

boy took bat home

dog brought hat home

-d選項僅顯示檔案中連續重複出現的行。

****************************************====

[root@stu100 ~]# uniq -u test

girl took bat home

-u選項顯示檔案中沒有連續出現的行。

****************************************====

[root@stu100 ~]# uniq -f 2 -s 2 test

boy took bat home

忽略每行的前2個字段,忽略第二 個空白字元和第三個欄位的首字元,結果at home

****************************************====

[root@stu100 ~]# uniq -f 1 test

boy took bat home

dog brought hat home

忽 略每行的第乙個字段,這樣boy ,girl開頭的行看起來是連續重複的行。

****************************************====

[root@stu100 ~]# uniq -d test

boy took bat home

boy took bat home

boy took bat home

boy took bat home

dog brought hat home

dog brought hat home

dog brought hat home

顯示所有重複的行,每個重複的行都顯示

linux uniq命令詳解

linux uniq命令詳解 用uniq命令可以刪除相鄰的重複行 uniq file 但如果一文字中有重複卻不相鄰的行則無法刪除,需要結合sort命令 sort file uniq 等效的sort命令是 sort u file 另外uniq命令有4個有用的選項 uniq d file 只輸出file...

linux uniq 命令詳解

uniq 命令 文字 uniq 是linux命令 用途 報告或刪除檔案中重複的行。語法 uniq c d u f fields s characters fields characters infile outfile 描述 uniq 命令刪除檔案中的重複行。uniq 命令讀取由 infile 引數...

linux uniq 命令詳解

uniq 命令 文字 uniq 是linux命令 用途 報告或刪除檔案中重複的行。語法 uniq c d u f fields s characters fields characters infile outfile 描述 uniq 命令刪除檔案中的重複行。uniq 命令讀取由 infile 引數...