linux用命令刪除重復行


文本處理時,經常要刪除重復行,下面是三種方法

 

第一,用sort+uniq,注意,單純uniq是不行的。

sort -n test.txt | uniq

 

第二,用sort+awk命令,注意,單純awk同樣不行,原因同上。

sort -n $file | awk '{if($0!=line)print; line=$0}'

 

第三,用sort+sed命令,同樣需要sort命令先排序。

sort -n $file | sed '$!N; /^\(.*\)\n\1$/!P; D'

 

Shell腳本

view plainprint?

# !/bin/sh 

 

 

file='test.txt' 

 

sort -n $file | uniq 

 

sort -n $file | awk '{if($0!=line)print; line=$0}' 

 

sort -n $file | sed '$!N; /^\(.*\)\n\1$/!P; D' 

測試文件:

yanggang@barry$ cat test.txt

aaa

bbbbb

ccccc

123

aaaaa

123

bbb

aaa

執行結果:

yanggang@barry$ ./diffRow.sh

aaa

aaaaa

bbb

bbbbb

ccccc

123

 

文章轉自:http://www.2cto.com/os/201111/109911.html


免責聲明!

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



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