Linux uniq命令怎么使用

linux uniq 命令用于檢查及刪除文本文件中重復(fù)出現(xiàn)的行列,一般與 sort 命令結(jié)合使用。

uniq 可檢查文本文件中重復(fù)出現(xiàn)的行列。

語法:

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

參數(shù):

-c或–count 在每列旁邊顯示該行重復(fù)出現(xiàn)的次數(shù)。

-d或–repeated 僅顯示重復(fù)出現(xiàn)的行列。

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

-s或–skip-Chars= 忽略比較指定的字符。

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

-w或–check-chars= 指定要比較的字符。

–help 顯示幫助。

–version 顯示版本信息。

[輸入文件] 指定已排序好的文本文件。如果不指定此項(xiàng),則從標(biāo)準(zhǔn)讀取數(shù)據(jù);

[輸出文件] 指定輸出的文件。如果不指定此選項(xiàng),則將內(nèi)容顯示到標(biāo)準(zhǔn)輸出設(shè)備(顯示終端)。

實(shí)例:

文件testfile中第 2、3、5、6、7、9行為相同的行,使用 uniq 命令刪除重復(fù)的行,可使用以下命令:

uniq

testfile中的原有內(nèi)容為:

$ cat testfile      #原有內(nèi)容   test 30   test 30   test 30   Hello 95   Hello 95   Hello 95   Hello 95   Linux 85   Linux 85

使用uniq 命令刪除重復(fù)的行后,有如下輸出結(jié)果:

$ uniq testfile     #刪除重復(fù)行后的內(nèi)容   test 30   Hello 95   Linux 85

檢查文件并刪除文件中重復(fù)出現(xiàn)的行,并在行首顯示該行重復(fù)出現(xiàn)的次數(shù)。使用如下命令:

uniq

結(jié)果輸出如下:

$ uniq -c testfile      #刪除重復(fù)行后的內(nèi)容   3 test 30             #前面的數(shù)字的意義為該行共出現(xiàn)了3次   4 Hello 95            #前面的數(shù)字的意義為該行共出現(xiàn)了4次   2 Linux 85

當(dāng)重復(fù)的行并不相鄰時(shí),uniq 命令是不起作用的,即若文件內(nèi)容為以下時(shí),uniq 命令不起作用:

$ cat testfile1      # 原有內(nèi)容  test 30   Hello 95   Linux 85  test 30   Hello 95   Linux 85  test 30   Hello 95   Linux 85

這時(shí)我們就可以使用 sort:

$ sort  testfile1 | uniq Hello 95   Linux 85  test 30

統(tǒng)計(jì)各行在文件中出現(xiàn)的次數(shù):

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

在文件中找出重復(fù)的行:

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

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊12 分享