如何判断文件是否有重复的行?------ sort和uniq搞起

来源:互联网 发布:成龙功夫怎么样 知乎 编辑:程序博客网 时间:2024/05/29 13:30

        如何判断文件是否有重复的行? Windows程序员估计要歇菜了, 还是省省吧, linux搞起:

taoge@localhost test> cat a.txt xxx111xxxtaoge@localhost test> wc -l a.txt 3 a.txttaoge@localhost test> taoge@localhost test> taoge@localhost test> taoge@localhost test> sort a.txt | uniq > b.txttaoge@localhost test> wc -l b.txt 2 b.txttaoge@localhost test> 

        文件由3行变成了2行, 所以a.txt中有重复的行。 

        再看:

taoge@localhost test> cat c.txt xxx111xxx111taoge@localhost test> wc -l c.txt 3 c.txttaoge@localhost test> taoge@localhost test> taoge@localhost test> taoge@localhost test> sort c.txt | uniq > d.txttaoge@localhost test> wc -l d.txt 3 d.txttaoge@localhost test> 

        行数没有变化, 可见c.txt中没有重复的行。


        当然, 还有更简单的方法,如下:

taoge@localhost test> sort a.txt | uniq -dxxxtaoge@localhost test> sort c.txt | uniq -dtaoge@localhost test> 
         其中, uniq -d是输出duplicate了的行, 也就是重复的行。 要注意, 这是建立在sort后的基础之上的。

       

        在实际开发中, 要经常处理类似问题, 那些还在想怎么去写程序来判断的朋友, 可以换个角度考虑一下了。







0 0