linux命令(12)uniq去重


轉載地址:http://blog.51yip.com/shell/1022.html

實例詳細說明linux下去除重復行命令uniq

一,uniq干什么用的

文本中的重復行,基本上不是我們所要的,所以就要去除掉。linux下有其他命令可以去除重復行,但是我覺得uniq還是比較方便的一個。使用uniq的時候要注意以下二點

1,對文本操作時,它一般會和sort命令進行組合使用,因為uniq 不會檢查重復的行,除非它們是相鄰的行。如果您想先對輸入排序,使用sort -u。

2,對文本操作時,若域中為先空字符(通常包括空格以及制表符),然后非空字符,域中字符前的空字符將被跳過

二,uniq參數說明

 1 [zhangy@BlackGhost ~]$ uniq --help
 2 用法:uniq [選項]... [文件]
 3 從輸入文件或者標准輸入中篩選相鄰的匹配行並寫入到輸出文件或標准輸出。
 4 
 5 不附加任何選項時匹配行將在首次出現處被合並。
 6 
 7 長選項必須使用的參數對於短選項時也是必需使用的。
 8  -c, --count              //在每行前加上表示相應行目出現次數的前綴編號
 9  -d, --repeated          //只輸出重復的行
10  -D, --all-repeated      //只輸出重復的行,不過有幾行輸出幾行
11  -f, --skip-fields=N     //-f 忽略的段數,-f 1 忽略第一段
12  -i, --ignore-case       //不區分大小寫
13  -s, --skip-chars=N      //根-f有點像,不過-s是忽略,后面多少個字符 -s 5就忽略后面5個字符
14  -u, --unique            //去除重復的后,全部顯示出來,根mysql的distinct功能上有點像
15  -z, --zero-terminated   end lines with 0 byte, not newline
16  -w, --check-chars=N      //對每行第N 個字符以后的內容不作對照
17  --help              //顯示此幫助信息並退出
18  --version              //顯示版本信息並退出

三,測試文本文件uniqtest

 1     this is a test  
 2     this is a test  
 3     this is a test  
 4     i am tank  
 5     i love tank  
 6     i love tank  
 7     this is a test  
 8     whom have a try  
 9     WhoM have a try  
10     you  have a try  
11     i want to abroad  
12     those are good men  
13     we are good men 

四,實例詳解

從例子中我們可以看出,uniq的一個特性,檢查重復行的時候,只會檢查相鄰的行。重復數據,肯定有很多不是相鄰在一起的。

 [zhangy@BlackGhost mytest]$ uniq -c uniqtest  
     3 this is a test  
     1 i am tank  
     2 i love tank  
     1 this is a test           //和第一行是重復的  
     1 whom have a try  
     1 WhoM have a try  
     1 you  have a try  
     1 i want to abroad  
     1 those are good men  
     1 we are good men  

這樣就可以解決上個例子中提到的問題

    [zhangy@BlackGhost mytest]$ sort uniqtest |uniq -c  
     1 WhoM have a try  
     1 i am tank  
     2 i love tank  
     1 i want to abroad  
     4 this is a test  
     1 those are good men  
     1 we are good men  
     1 whom have a try  
     1 you  have a try  

uniq -d 只顯示重復的行

[zhangy@BlackGhost mytest]$ uniq -d -c uniqtest  
 3 this is a test  
 2 i love tank 

uniq -D 只顯示重復的行,並且把重復幾行都顯示出來。他不能和-c一起使用

[zhangy@BlackGhost mytest]$ uniq -D uniqtest  
this is a test  
this is a test  
this is a test  
i love tank  
i love tank 

在這里those只有一行,顯示的卻是重復了,這是因為,-f 1 忽略了第一列,檢查重復從第二字段開始的。

[zhangy@BlackGhost mytest]$ uniq -f 1 -c uniqtest  
 3 this is a test  
 1 i am tank  
 2 i love tank  
 1 this is a test  
 2 whom have a try  
 1 you  have a try  
 1 i want to abroad  
 2 those are good men   //只有一行,顯示二行

檢查的時候,不區分大小寫

[zhangy@BlackGhost mytest]$ uniq -i -c uniqtest  
 3 this is a test  
 1 i am tank  
 2 i love tank  
 1 this is a test  
 2 whom have a try  //一個大寫,一個小寫  
 1 you  have a try  
 1 i want to abroad  
 1 those are good men  
 1 we are good men

檢查的時候,不考慮前4個字符,這樣whom have a try 就和 you have a try 就一樣了。

[zhangy@BlackGhost mytest]$ uniq -s 4 -c uniqtest  
3 this is a test  
1 i am tank  
2 i love tank  
1 this is a test  
3 whom have a try   //根上一個例子有什么不同  
1 i want to abroad  
1 those are good men  
1 we are good men 

 去重復的項,然后全部顯示出來

    [zhangy@BlackGhost mytest]$ uniq -u uniqtest  
    i am tank  
    this is a test  
    whom have a try  
    WhoM have a try  
    you  have a try  
    i want to abroad  
    those are good men  
    we are good men  

對每行第2個字符以后的內容不作檢查,所以i am tank 根 i love tank就一樣了。

[zhangy@BlackGhost mytest]$ uniq -w 2 -c uniqtest  
 3 this is a test  
 3 i am tank  
 1 this is a test  
 1 whom have a try  
 1 WhoM have a try  
 1 you  have a try  
 1 i want to abroad  
 1 those are good men  
 1 we are good men 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM