Linux命令或脚本在文本文件中计数重复的行? [英] Linux command or script counting duplicated lines in a text file?

查看:294
本文介绍了Linux命令或脚本在文本文件中计数重复的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个包含以下conent的文本文件

If I have a text file with the following conent

red apple
green apple
green apple
orange
orange
orange

是否有一个Linux命令或脚本,我可以使用以获得以下结果?

Is there a Linux command or script that I can use to get the following result?

1 red apple
2 green apple
3 orange


推荐答案

通过排序(将相邻项目放在一起),然后 uniq -c 给出计数,即:

Send it through sort (to put adjacent items together) then uniq -c to give counts, i.e.:

sort filename | uniq -c

并按排序顺序(按频率)获取该列表,您可以

and to get that list in sorted order (by frequency) you can

sort filename | uniq -c | sort -nr

这篇关于Linux命令或脚本在文本文件中计数重复的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆