how would you calculate percentage by per line? Given a column of 16 lines, grab each line and divide it by the sum of the entire column and multiply by 100?
thanks ...
What have you attempted so far and and where do you get stuck?
Regards
well i am very new to awk. i just managed to get the sum of a field.
awk '{ for (i = 1; i <= NF; i++) s = s+$i }; END { print s /100 }'
i can remove the percentage of the whole field as one.. but i am stuck at processing the percentage for each line of that column
It's not really clear what your trying to achieve, can you post your input file and the desired output?
Regards
Input :
629
587
584
output :
34.94
16.85
16.77
(what i am doing here is taking the first line 629 divide it by sum of the field - 1800 and multiplying to give a % of 34.94)
I can get the sum of the field... like i have stated in the code.. but cannot calculate the percentage of each line of the field.
This is ok:
34.94 = 629/1800*100
But can you clarify this?
16.85 = <- I can't figure this out
16.77 = <- ??
Regards
awk '{array[NR] = $0; sum+= $0 }
END {
for (x = 1; x <= NR; x++)
printf "%2.2f\n", (100 * array[x])/sum
}
' file
sorry they were the wrong values
32.61
32.44
i hope you are understanding what i am getting at. i need to manipulate each line and show them as percentage
awk 'NR==FNR{s+=$0;next}{printf("%2.2f\n", $0/s*100}' file file
Regards