linux怎么去重统计

linux命令行提供了非常强大的文本处理功能,组合利用linux命令能实现好多强大的功能。本文这里举例说明如何利用linux命令行进行文本按行去重并按重复次数排序。主要用到的命令有sort,uniq和cut。其中,sort主要功能是排序,uniq主要功能是实现相邻文本行的去重,cut可以从文本行中提取相应的文本列(简单地说,就是按列操作文本行)。

linux怎么去重统计

文本行去重并按重复次数排序 

例:

首先,对文本行进行去重并统计重复次数(uniq命令加-c选项可以实现对重复次数进行统计)。 

$ sort test.txt | uniq -c  2 Apple and Nokia.  4 Hello World.  1 I wanna buy an Apple device.  1 My name is Friendfish.  2 The Iphone of Apple company.

对文本行按重复次数进行排序。 

sort -n可以识别每行开头的数字,并按其大小对文本行进行排序。默认是按升序排列,如果想要按降序要加-r选项(sort -rn)。 

$ sort test.txt | uniq -c | sort -rn  4 Hello World.  2 The Iphone of Apple company.  2 Apple and Nokia.  1 My name is Friendfish.

每行前面的删除重复次数。 

cut命令可以按列操作文本行。可以看出前面的重复次数占8个字符,因此,可以用命令cut -c 9- 取出每行第9个及其以后的字符。

$ sort test.txt | uniq -c | sort -rn | cut -c 9-  Hello World.  The Iphone of Apple company.  Apple and Nokia.  My name is Friendfish.  I wanna buy an Apple device.

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享