庆典:从频率表中获取的百分比 [英] bash: getting percentage from a frequency table

查看:80
本文介绍了庆典:从频率表中获取的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经以获得在一个文件中的某一列项的频率做了一个小bash脚本

将输出某事像这样

  A 30
乙25
C的20
D 15-
E-10

我的脚本中使用的命令是这样的。

 切-f $ 1 $ 2 |排序| uniq的-c |
排序-r -k1,1 -n | AWK'{printf的%-20s%-15d \\ n,$ 2,$ 1}

如何修改它显示了每种情况的相对百分比为好。所以它会像

  A 30 30%
乙25 25%
C的20 20%
D 15- 15%
E-10的10%


解决方案

试试这个(与排序移到结尾:

 切-f $ 1 $ 2 |排序| uniq的-c | AWK'{阵列[$ 2] = $ 1;总和+ = $ 1} END {了(我在阵列)的printf%-20s%-15d%6.2f %% \\ n,我,阵列[我],数组[I] /总和* 100}'|排序-r -k2,2 -n

i had made a small bash script in order to get the frequency of items in a certain column of a file.

The output would be sth like this

A     30
B     25
C     20
D     15
E     10

the command i used inside the script is like this

cut -f $1 $2| sort | uniq -c | 
sort -r -k1,1 -n | awk '{printf "%-20s %-15d\n", $2,$1}'

how can i modify it to show the relative percentages for each case as well. so it would be like

A     30     30%
B     25     25%
C     20     20% 
D     15     15%
E     10     10%

解决方案

Try this (with the sort moved to the end:

cut -f $1 $2| sort | uniq -c  | awk '{array[$2]=$1; sum+=$1} END { for (i in array) printf "%-20s %-15d %6.2f%%\n", i, array[i], array[i]/sum*100}' | sort -r -k2,2 -n

这篇关于庆典:从频率表中获取的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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