sum and proportion calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sum and proportion calculation
# 1  
Old 10-22-2012
sum and proportion calculation

I have a file like this:

HTML Code:
ASSLRADTQY 319895
SAIVVGGSNQPQH 252661
ASSLDRGEIQPQH 237702
ASSYSPGGWTDTQY 106598
ASRMTGRATEAF 70363
SARDTRGEQY 69429
ASSLALDSEAF 58879
SATTWTSGLLDTQY 46100
SASAGVGYTEAF 45721
ASSLERGRVDEQF 45339
ASSLSSNTEAF 38771
ASSVANTGELF 35470
ASSLVNTGELF 34085
ASSDGTNQPQH 33441
ASSYGTGWQETQY 28675
SARQGDTEAF 28296
ASSLEQGARTDTQY 27746
ASSLGRSLREQF 23784
ASSESGSSTDTQY 23691
SASTRLSDRTTDLLYGYT 23462
I use the following code to get the sum of $2

Code:
awk '{sum += $2} {print sum}' file

The file is sorted according to $2. I want know how many records' value of $2 contribute to 10% of the sum value.

Thank you!
# 2  
Old 10-22-2012
Quote:
how many records' value of $2 contribute to 10% of the sum value.
How do you propose to do that? Start from the top and keep summing until you get to 10%? Start from the highest value and go down? Each selection method will give a different answer.

Here is one way of many
Code:
total=$(( `awk '{sum += $2} {print sum}' file` / 10 ))
awk -v tot=$total 'sum < tot {sum+=$2} sum>=tot {print NR; exit}' file

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 10-22-2012
Quote:
Originally Posted by jim mcnamara
How do you propose to do that? Start from the top and keep summing until you get to 10%? Start from the highest value and go down? Each selection method will give a different answer.

Here is one way of many
Code:
total=$(( `awk '{sum += $2} {print sum}' file` / 10 ))
awk -v tot=$total 'sum < tot {sum+=$2} sum>=tot {print NR; exit}' file

The file have sorted the highest value form the top, so it should be work.

When i run the code above, it shows the following information:

HTML Code:
syntax error: operand expected (error token is "'awk '{sum+=} {print sum}' output.txt'/10 ")
But I use
Code:
awk '{sum+=$2} {print sum}' output.txt'/10

in my script.
# 4  
Old 10-22-2012
Code:
awk 'FNR==NR{s+=$2;next} (s1+=$2) >=(s/10) {print FNR;exit}' myFile myFile

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 10-22-2012
Quote:
Originally Posted by vgersh99
Code:
awk 'FNR==NR{s+=$2;next} (s1+=$2) >=(s/10) {print FNR;exit}' myFile myFile

NICE code! Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Duration Calculation

I have 2 variables startTime='122717 23:20' endTime='122817 0:40' how can i get the elapsed duration as like "1 hour 20 minutes" ? (8 Replies)
Discussion started by: vikram3.r
8 Replies

2. Shell Programming and Scripting

Calculation

Hi, i have a large file like this: Contig1 1 5 Contig1 2 4 Contig1 3 3 Contig1 4 5 Contig1 5 3 Contig1 6 4 Contig2 1 3 Contig2 2 7 Contig2 3 2 Contig2 4 9 Contig2 5 10 Contig2 6 3 Contig2 7 7 Contig2 8 2 Contig2 9 7 Contig2 10 5 contig1 2 4 contig1 3 3 contig1 4 5 (3 Replies)
Discussion started by: the_simpsons
3 Replies

3. Shell Programming and Scripting

VG calculation in GB

for i in `lsvg` do echo "VG Name:" $i echo "Total VG Size:" lsvg $i |grep "TOTAL PPs:" |awk '{print $7}' | cut -c2- echo "Free VG Size:" lsvg $i |grep "FREE PPs:" | awk '{print $7}' | cut -c2- done The PP Sizes are in MB. I like to have the sizes in GB. Also, I like to have the... (14 Replies)
Discussion started by: Daniel Gate
14 Replies

4. Shell Programming and Scripting

Calculation returns no value

#/bin/sh ..... #convert memory to MB let "mmsize_a= ($mmsize)/256" let "mminuse_a= ($mminuse)/256" let "mmfree_a= ($mmsize_a -$mminuse_a)" let "mmfreepercent= (($mmfree_a)/($mmsize_a))*100" # #format output echo "\n\n######################" >>$sndFile echo "\n$sysName Total Memory usage"... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

5. Shell Programming and Scripting

Calculation with floats

I want to make computations using floats. This cannot be done with csh. ksh seem to have a problem as well. What is good shell for computations without having to resort to bc or awk? How about python? (5 Replies)
Discussion started by: kristinu
5 Replies

6. Shell Programming and Scripting

Print sum and relative value of the sum

Hi i data looks like this: student 1 Subject1 45 55 Subject2 44 55 Subject3 33 44 // student 2 Subject1 45 55 Subject2 44 55 Subject3 33 44 i would like to sum $2, $3 (marks) and divide each entry in $2 and $3 with their respective sums and print for each student as $4 and... (2 Replies)
Discussion started by: saint2006
2 Replies

7. Shell Programming and Scripting

calculation

Could someone till me what this calculation really means let foo=`date "+(1%H-106)*60+1%M-100"` bar=foo+1440 (4 Replies)
Discussion started by: freddie999
4 Replies

8. Shell Programming and Scripting

duration calculation

I have a file which has 3 coloumns emp_name, Joining_date, Designation. abc 12/1/2001 SSE def 2/25/2007 SE ghi 3/18/2009 SA abc 8/1/2008 SSE def 2/13/2007 SE ghi 3/24/2005 SA I need to find out the emp who has been in the company for longest period(Till date). Can I have any... (3 Replies)
Discussion started by: siba.s.nayak
3 Replies

9. Shell Programming and Scripting

calculation

Hi, I am in ksh88 I am trying to get the result of the calculation using 3 variables: TOTAL CAPACITY and get the following error: $DB_CAPACITY=(( $DB_SIZE * 100 / $TOTAL )) ksh: syntax error: `((' unexpected I cannot figure out what am I doing wrong... Thanks for any help -A (2 Replies)
Discussion started by: aoussenko
2 Replies

10. Linux

memory calculation

hi all, sorry for silly question. but i have a doubt in calculation of memory utilization calculation. using 'free' command we calculate the memory utilization as used/total. but my friend said that its wrong and we have to include cached also as (used-cached)/memory. but these... (0 Replies)
Discussion started by: rrlog
0 Replies
Login or Register to Ask a Question