首頁  >  文章  >  運維  >  Linux uniq指令怎麼使用

Linux uniq指令怎麼使用

WBOY
WBOY轉載
2023-05-16 19:05:101617瀏覽

Linux uniq 指令用於檢查及刪除文字檔案中重複出現的行列,一般與 sort 指令結合使用。

uniq 可檢查文字檔案中重複出現的行列。

語法:

uniq [-cdu][-f<栏位>][-s<字符位置>][-w<字符位置>][--help][--version][输入文件][输出文件]

參數:

-c或--count 在每列旁邊顯示該行重複出現的次數。

-d或--repeated 僅顯示重複出現的行列。

-f<欄位>或--skip-fields=<欄位> 忽略比較指定的欄位。

-s<字元位置>或--skip-chars=<字元位置> 忽略比較指定的字元。

-u或--unique 僅顯示出一次的行列。

-w<字元位置>或--check-chars=<字元位置> 指定要比較的字元。

--help 顯示幫助。

--version 顯示版本資訊。

[輸入檔] 指定已排序好的文字檔。如果不指定此項,則從標準讀取資料;

[輸出檔案] 指定輸出的檔案。如果不指定此選項,則將內容顯示至標準輸出裝置(顯示終端)。

實例:

檔案testfile中第2、3、5、6、7、9行為相同的行,使用uniq 指令刪除重複的行,可使用以下指令:

uniq

testfile中的原有內容為:

$ cat testfile      #原有内容  
test 30  
test 30  
test 30  
Hello 95  
Hello 95  
Hello 95  
Hello 95  
Linux 85  
Linux 85

使用uniq 指令刪除重複的行後,有下列輸出結果:

$ uniq testfile     #删除重复行后的内容  
test 30  
Hello 95  
Linux 85

檢查檔案並刪除文件中重複出現的行,並在行首顯示該行重複出現的次數。使用以下指令:

uniq

結果輸出如下:

$ uniq -c testfile      #删除重复行后的内容  
3 test 30             #前面的数字的意义为该行共出现了3次  
4 Hello 95            #前面的数字的意义为该行共出现了4次  
2 Linux 85

當重複的行並不相鄰時,uniq 指令是不起作用的,即若檔案內容為以下時,uniq 指令不行:

$ cat testfile1      # 原有内容 
test 30  
Hello 95  
Linux 85 
test 30  
Hello 95  
Linux 85 
test 30  
Hello 95  
Linux 85

這時我們就可以使用sort:

$ sort  testfile1 | uniq
Hello 95  
Linux 85 
test 30

統計各行在檔案中出現的次數:

$ sort testfile1 | uniq -c
   3 Hello 95  
   3 Linux 85 
   3 test 30

在檔案中找出重複的行:

$ sort testfile1 | uniq -d
Hello 95  
Linux 85 
test 30

以上是Linux uniq指令怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除