Percentage of occurence

Dear all,
I have data like below and i need to add coloumn before the COUNT field to see the Percentage out of all COUNT field value for respective raw.

=============================================
COUNT           CODE         sConnType                  tConnType
=============================================
265873          171          uehRrcConn                 uehPacketEulHs
239103          171          uehPacketCch               uehPacketEulHs
56746           500          uehRrcConn                 uehPacketEulHs
51734           171          uehPacketEulHs             uehPacketEulHs
28244           54           uehPacketCch               uehPacketEulHs
22422           171          uehPacket128Hs             uehPacket384Hs

Expected format

=============================================
PERCENTAGE COUNT           CODE         sConnType                  tConnType
=============================================
    X                  265873          171          uehRrcConn                 uehPacketEulHs
                       239103          171          uehPacketCch               uehPacketEulHs
                       56746           500          uehRrcConn                 uehPacketEulHs
                       51734           171          uehPacketEulHs             uehPacketEulHs
                       28244           54           uehPacketCch               uehPacketEulHs
                       22422           171          uehPacket128Hs           uehPacket384Hs

Where X = 100*265873/(265873+239103+56746+51734+28244+22422)

Thanks in advance.

Rgds,
Iroshan

What have you tried so far?

In the below code reading file twice

  awk 'BEGIN{print "PERCENTAGE COUNT CODE sConnType tConnType"}
    NR==FNR && NR>1{sum+=$1;next}
    {if(FNR>1){print 100*$1/sum,$0}}' file.txt file.txt

So far, i didn't have a logic to print that. I only can get Text file like this from processing TEXT files using "awk/sed/sort/uniq/grep" commands.
Hope UNIX command can do that.

Rgds,
Iroshan

Ok, then I reinstate the post from pravin27 (post #3, which I had previously hidden).

If you don't understand any part of it, it's important that you say.

Repliers, please note: documented solutions are encouraged!

Thanks for the solution it works for me as i Expected.

Rgds,
Iroshan

Hi Scott,

I will take care going forward.. Apologies..

Thanks
Pravin

1 Like