How to calculate what percentage of X value is there in the file? | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to calculate what percentage of X value is there in the file?

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-17-2012
aroragaurav.84 aroragaurav.84 is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 6 November 2013, 4:34 PM EST
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
How to calculate what percentage of X value is there in the file?

Input File:

Code:
5081
2058
175
8282
2358
7347
6612
3459

END OF INPUT FILE

I need to know how to calculate minimum,maximum,average of the values in the file and also what percentage is the values over some user defined value for example 1000 and what percentage of value is over 5000.

By using the following command mentioned below i was able to find the minimum ,maximum and average but having problem in calculatung perctangae.


Code:
awk 'NR==1{min=$1;max=$1;sum+=$1;next}{if ($1<min) min=$1;if ($1>max)
  max=$1;sum+=$1}END{print "min: "min", max: "max",NR: "NR", avg: "sum/NR}' INPUT FILE

Moderator's Comments:
Your Summary - Threads Created: 9 - Posts: 17 - Posts Edited by a Moderator: 13

Last edited by Scott; 12-17-2012 at 02:39 PM.. Reason: Code tags!
Sponsored Links
    #2  
Old Unix and Linux 12-17-2012
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 30 March 2015, 11:20 AM EDT
Location: Galactic Empire
Posts: 3,401
Thanks: 240
Thanked 1,215 Times in 1,141 Posts

Code:
awk ' BEGIN { max=sum=0; th=fv=0; } NR==1 { min=$1; } {
 val=$1;
 sum+=val;
 if(val<min) { min=val; }
 if(val>max) { max=val; }
 if(val>1000) { th++; }
 if(val>5000) { fv++; }
 } END {
 printf "Min: %d Max: %d Avg: %d 1000 per: %d 5000 per: %d\n", min, max, (sum/NR), (th/NR * 100), (fv/NR * 100);
} ' filename

Sponsored Links
    #3  
Old Unix and Linux 12-17-2012
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is offline Forum Staff  
Moderator
 
Join Date: Oct 2010
Last Activity: 31 March 2015, 6:29 PM EDT
Posts: 3,124
Thanks: 121
Thanked 1,024 Times in 958 Posts
Here is a version where you can Set limits for F1 and F2 on the command line:


Code:
awk -vF1=1000 -vF2=5000 'FNR==1 {min=$1}
{
 sum+=$1;
 if($1<min) min=$1
 if($1>max) max=$1
 th+=($1>F1)
 fv+=($1>F2)
}
END {
 printf "Min: %d Max: %d Avg: %d above %d: %d%% above %d: %d%%\n", min, max, sum/NR, F1, th/NR*100, F2, fv/NR * 100
} ' infile

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script shell, how to calculate percentage? likeaix Shell Programming and Scripting 3 08-04-2011 10:30 AM
Need an AWK script to calculate the percentage bobprabhu Shell Programming and Scripting 8 11-07-2008 04:42 AM
How can i calculate percentage ?? bobprabhu Shell Programming and Scripting 6 10-31-2008 09:37 AM
How to calculate the percentage for the values in column saleru_raja Shell Programming and Scripting 3 08-29-2008 03:15 AM
how do I calculate percentage ? the_learner Programming 6 04-18-2007 04:40 PM



All times are GMT -4. The time now is 09:08 PM.