Latest web development tutorials

Linux uniq命令

Linux 命令大全 Linux命令大全

Linux uniq命令用於檢查及刪除文本文件中重複出現的行列。

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 行、第5 行、第9 行為相同的行,使用uniq 命令刪除重複的行,可使用以下命令:

uniq testfile 

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-c testfile 

結果輸出如下:

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

Linux 命令大全 Linux命令大全