Go Back   The UNIX and Linux Forums > Special Forums > UNIX Desktop for Dummies Questions & Answers


UNIX Desktop for Dummies Questions & Answers Discuss UNIX and Linux user interfaces like GNOME, KDE, CDE, and Open Office here. All UNIX and Linux Newbies Welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-11-2012
Registered User
 
Join Date: Jul 2010
Posts: 39
Thanks: 16
Thanked 0 Times in 0 Posts
Calculate average for rows in a text file

Dear Gurus,

I have tab-delimited text files with matrix containing values. The first column is a identifier and other columns have the corresponding values. I would like to calculate the average value (total number/number of entries) for all entries from 2nd column to the last column in row wise.

For example,

Code:
file 1
Id per1 per2 per3 
12 34.56 -0.54 12.5 
13 0.15 -0.06 0.05 
14 0.67 0.12 0.09 
15 0.05 0.06 -0.07

Please note some values are negative numbers. I want to have a result file

Code:
ID avg
12 15.50
13 0.14 
14 0.88
15 0.04

Can somebody suggest a script to deal with this? Thanks a lot indeed.
Moderator's Comments:
code tags please

Last edited by jim mcnamara; 07-11-2012 at 11:26 AM..
Sponsored Links
    #2  
Old 07-11-2012
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,652
Thanks: 164
Thanked 644 Times in 621 Posts

Code:
awk 'NR==1{next}
       {printf("%s\t", $1  
        printf("%.2f\n", ($2 + $3 + $4)/3 }'  file1 > newfile

The Following User Says Thank You to jim mcnamara For This Useful Post:
Unilearn (07-12-2012)
Sponsored Links
    #3  
Old 07-11-2012
Registered User
 
Join Date: Jul 2010
Posts: 39
Thanks: 16
Thanked 0 Times in 0 Posts
thanks a lot for the script Jim. It works if I have three columns to calculate. How can do it if the file has more than 100 columns? Is there a way to mention the range like column 1 to column 150 for example?
    #4  
Old 07-11-2012
Mead Rotor
 
Join Date: Aug 2005
Location: Saskatchewan
Posts: 16,373
Thanks: 491
Thanked 2,535 Times in 2,418 Posts
Best to ask for what you need on the first go

Code:
awk 'NR==1 { next }
        { T=0
           for(N=2; N<=NF; N++) T+=$N;
           T/=(NF-1)
           print $1, T }' file > outfile

That will calculate all columns from the second column to the last. You could also have FIRST=2, LAST=150, and for(N=FIRST; N<=LAST; N++), then divide by (FIRST-LAST)
The Following User Says Thank You to Corona688 For This Useful Post:
Unilearn (07-12-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calculate average from CSV file using PERL script sayachop Shell Programming and Scripting 24 09-06-2011 01:02 AM
Calculate Average AWK AriasFco Shell Programming and Scripting 2 05-26-2011 05:57 PM
AWK novice - calculate the average alex2005 Shell Programming and Scripting 6 11-05-2010 10:26 AM
Calculate average of each of position of contents in a huge file patrick87 Shell Programming and Scripting 7 10-18-2009 10:12 PM
calculate average cdfd123 Programming 3 11-13-2008 11:23 AM



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