|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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.
Last edited by jim mcnamara; 07-11-2012 at 11:26 AM.. |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 > outfileThat 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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|