awk percentage


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk percentage
# 1  
Old 09-28-2008
awk percentage

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 ...
# 2  
Old 09-28-2008
What have you attempted so far and and where do you get stuck?

Regards
# 3  
Old 09-28-2008
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

Last edited by rockiefx; 09-28-2008 at 07:18 AM..
# 4  
Old 09-28-2008
It's not really clear what your trying to achieve, can you post your input file and the desired output?

Regards
# 5  
Old 09-28-2008
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.
# 6  
Old 09-28-2008
This is ok:

34.94 = 629/1800*100

But can you clarify this?

16.85 = <- I can't figure this out
16.77 = <- ??

Regards
# 7  
Old 09-28-2008
Code:
awk '{array[NR] = $0; sum+= $0 }
     END {
       for (x = 1; x <= NR; x++)
             printf "%2.2f\n", (100 * array[x])/sum
     }
   ' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Shell /awk script for Percentage

having two columns, A and B.. i need to add another column C in a file and calculate the percentage based on the column A and B. (COLUMN B/ COLUMN A *100) . "|" is delimiter separating the A and B.. need C column with the percentage value. Thanks for your help 100|50 |50% ... (6 Replies)
Discussion started by: kartikirans
6 Replies

2. Shell Programming and Scripting

Help with awk percentage calculation from a file

i have a file say test with the below mentioned details Folder Name Total space Space used /test/test1 500.1GB 112.0 GB /test/test2 3.2 TB 5TB /test/test3 3TB 100GB i need to calculate percentage of each row based on total space and space used and copy... (9 Replies)
Discussion started by: venkitesh
9 Replies

3. Shell Programming and Scripting

Percentage sign causing awk problems

looks like awk gets confused when there's a % next to a number. command im running: awk -F" " '/phxnaz001b/ && /vol/ && NF { if (($NF >= 80) && ($NF < 83)) { print ; print ; w++ } else if ($NF >= 83) { print ; c++ } } END { printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c) }' /tmp/test.log ... (3 Replies)
Discussion started by: SkySmart
3 Replies

4. Shell Programming and Scripting

awk to output the percentage of a field compared to length

The awk below using the sample input would output the following: Basically, it averages the text in $5 that matches if $7 < 30 . awk '{if(len==0){last=$5;total=$7;len=1;getline}if($5!=last){printf("%s\t%f\n", last,... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. Shell Programming and Scripting

Calculate percentage of columns greater than certain value in a matrix using awk

This matrix represents correlation values. Is it possible to calculate the percentage of columns (a1, a2, a3) that have a value >= |0.5| and report the percentage that has positive correlation >0.5 and negative correlation <-0.5 separately. thanx in advance! input name a1 a2 a3... (5 Replies)
Discussion started by: quincyjones
5 Replies

6. Shell Programming and Scripting

find percentage - awk

Please help me with this ... Input file /vol/test1 10G /vol/test2 1G /vol/test3 200G /vol/test4 3G Output File /vol/test1 10G - - 9G - /vol/test2 1024M - - 921M - /vol/test3 200G - - 180G - /vol/test4 3072M - - 2764M - Basically if Column 2 ( which is... (6 Replies)
Discussion started by: greycells
6 Replies

7. Shell Programming and Scripting

Need an awk script to calculate the percentage of value field and replace

Need an awk script to calculate the percentage of value field and replace I have a input file called file.txt with the following content: john|622.5674603562933|8|br:1;cn:3;fr:1;jp:1;us:2 andy|0.0|12|**:3;br:1;ca:2;de:2;dz:1;fr:2;nl:1 in fourth filed of input file, calulate percentage of each... (1 Reply)
Discussion started by: veeruasu
1 Replies

8. Shell Programming and Scripting

awk/sed percentage calculation

Hi all i have a text file with columns delimited with , 2010-08-18,10,24,.09751,39,7,14872,26732 . . . i would to add a extra column in the end with percentage calculation of columns 5 and 8 ie (39/26732)*100 so the output must look like ... (6 Replies)
Discussion started by: posner
6 Replies

9. Shell Programming and Scripting

awk or sed to determine battery percentage

So, I know this can be done but my awk and sed wizardry is not up to snuff for the job. Basically, I have tons and tons of Macbooks at my work and I am trying to grab the current percentage of the battery power on each client. If I use the system_profiler SPPowerDataType command it prints off... (4 Replies)
Discussion started by: tlarkin
4 Replies

10. Shell Programming and Scripting

Need an AWK script to calculate the percentage

Hi I need a awk script to calculate percentage. I have to pass the pararmeters in to the awk script and calculate the percentage. Sum = 50 passed = 43 failed = 7 I need to pass these value in to the awk script and calculate the percentage. Please advice me. (8 Replies)
Discussion started by: bobprabhu
8 Replies
Login or Register to Ask a Question