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

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

问题描述

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

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

推荐答案

通过 sort 发送(将相邻的项目放在一起)然后 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天全站免登陆