统计文本去重行数

项目github地址:bitcarmanlee easy-algorithm-interview-and-practice
欢迎大家star,留言,一起学习进步

常见的一个需求为:统计某个文本去重以后的行数

可以使用如下命令:

sort xxxfile | uniq | wc -l

也可以使用如下命令

sort -u xxxfile | wc -l

简单解释一下

其中sort -u的选项,解释如下

     -u, --unique
             Unique keys.  Suppress all lines that have a key that is equal to an already processed one.  This option, similarly to -s, implies a stable sort.  If used with -c or -C,
             sort also checks that there are no lines with duplicate keys.

可见sort的-u选项,就是自带去重功能。

而uniq 不会检查重复的行,除非它们是相邻的行,所以统计去重行数的时候,得先用sort排序,排序完了再用uniq去重,最后达到去重的目的。

猜你喜欢

转载自blog.csdn.net/bitcarmanlee/article/details/112124066