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

posted @ 2015-11-01 22:44  KoMiles  阅读(26405)  评论(1编辑  收藏  举报