bash: getting percentage from a frequency table

前端 未结 2 1394
粉色の甜心
粉色の甜心 2021-02-20 03:34

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
         


        
2条回答
  •  旧巷少年郎
    2021-02-20 04:11

    Change your awk command to something like this:

    awk '{ a[++n,1] = $2; a[n,2] = $1; t += $1 }
         END {
             for (i = 1; i <= n; i++)
                 printf "%-20s %-15d%d%%\n", a[i,1], a[i,2], 100 * a[i,2] / t
         }'
    

提交回复
热议问题