从文件生成频率表 [英] generating frequency table from file

查看:84
本文介绍了从文件生成频率表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个输入文件,每行包含一个数字,我如何计算该文件中有多少次出现?

Given an input file containing one single number per line, how could I get a count of how many times an item occurred in that file?

cat input.txt
1
2
1
3
1
0

所需的输出(=> [1,3,1,1]):

desired output (=>[1,3,1,1]):

cat output.txt
0 1
1 3
2 1
3 1

如果该解决方案也可以扩展为浮点数,那就太好了.

It would be great, if the solution could also be extended for floating numbers.

推荐答案

您的意思是您要计算一个项目在输入文件中出现多少次?首先对它进行排序(如果输入始终像示例中那样是数字,则使用-n),然后计算唯一结果.

You mean you want a count of how many times an item appears in the input file? First sort it (using -n if the input is always numbers as in your example) then count the unique results.

sort -n input.txt | uniq -c

这篇关于从文件生成频率表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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