The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 07:05 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
Use awk to calculate average of column 3 grossgermany UNIX for Dummies Questions & Answers 6 07-27-2007 02:29 PM
calculate average of column 2 onthetopo UNIX for Dummies Questions & Answers 0 07-06-2007 09:16 PM
how do I calculate percentage ? the_learner High Level Programming 6 04-18-2007 05:40 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-29-2008
saleru_raja saleru_raja is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 2
How to calculate the percentage for the values in column

Hi, I am having the file which contains the following two columns.
518 _factorial
256 _main
73 _atol
52 ___do_global_ctors
170 ___main
52 ___do_g
How can calculate the percentage of each value in the first column ?
first need to get the sum of the first column and then each value is divide by sum and multiply by 100 to get the percentage.
How can we write script for that ?

Thanks and Regards
Raja
  #2 (permalink)  
Old 08-29-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Try this:


Code:
awk '
        NR > max { max=NR }
        { tot+=$1; v[NR]=$1; d[NR]=$2 }
        END { for (i=1; i<=max; i++) { print v[i]*100/tot,v[i],d[i] } }

' inputfile

It prints the % as the first column.
  #3 (permalink)  
Old 08-29-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,345
Another approach:


Code:
awk 'NR==FNR{t+=$1;next}{printf("%.2f %s\n", $1/t*100, $2)}' file file

Regards
  #4 (permalink)  
Old 08-29-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Yep, that's much neater.
Closed Thread

Bookmarks

Tags
percentage columns

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0