easy-algorithm-interview-an.../tools/linux-shell/统计文本去重行数.md

797 B
Raw Blame History

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

可以使用如下命令:

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去重最后达到去重的目的。