Averaging multiple columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Averaging multiple columns
# 1  
Old 10-18-2009
Averaging multiple columns

Hello,

I am trying to average multiple columns simultaneously while skipping the first column.

I am using this awk line to average one column
Code:
awk '{sum+=$3} END { print "Average = ",sum/NR}'

But I want to be able to do it for multiple columns while skipping the first column. There are like 500 columns..

my file1 looks something like this:

Code:
jay  2  3  4
jon  2  3  4
jur  2  3  4
jam 2  3  4

I want the output to loo like this (which is the average of the file1 while skipping the first column)

Code:
2  3  4


thank you
# 2  
Old 10-18-2009
Code:
nawk '
  {for(i=2;i<=NF;i++) s[i]+=$i}
  END {for(i=2; i in s;i++) printf("%.2f%c", s[i]/NR, ((i+1) in s)?OFS:ORS)}
' myFile


Last edited by vgersh99; 10-18-2009 at 06:32 PM.. Reason: 'i<=NF' - thanks danmero
# 3  
Old 10-18-2009
thanks that is great

just curious, how would i make it skip the first two columns (instead of just the first)..

thanks
# 4  
Old 10-18-2009
Quote:
Originally Posted by gisele_l
just curious, how would i make it skip the first two columns (instead of just the first)..

thanks
Do you see anything in the code that deals with the columns?
Try experimenting a bit....
# 5  
Old 10-18-2009
Quote:
Originally Posted by gisele_l
just curious, how would i make it skip the first two columns (instead of just the first)..
Code:
awk '{for(i=c;++i<=NF;)s[i]+=$i}END{for(i=c;++i in s;)printf("%.2f%c",s[i]/NR,((i+1) in s)?FS:RS)}' c=2 file

Just a spin-up of vgersh99 solution Smilie
The value of c will set how many columns to crop from the beginning.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

2. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

3. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

4. Shell Programming and Scripting

Loop for row-wise averaging of multiple files using awk

Hello all, I need to compute a row-wise average of files with a single column based on the pattern of the filenames. I really appreciate any help on this. it would just be very difficult to do them manually as the rows are mounting to 100,000 lines. the filenames are as below with convention as... (2 Replies)
Discussion started by: ida1215
2 Replies

5. Shell Programming and Scripting

averaging specific column of multiple files

Hi all, I'm needing help again on scripting. I have weekly files with 3 columns, and I need to do monthly averaging on the values on column 3, the file naming convention is as follows: 20000105.u- 2000:year 01:month 05:day 20000112.u 20000119.u 20000126.u 20000202.u 20020209.u I need to... (15 Replies)
Discussion started by: ida1215
15 Replies

6. Shell Programming and Scripting

Averaging 3 files with multiple rows

Hi, I am trying to average the values from 3 files with the same format. They are very large files so I will describe the file and show some it of. Basically the file has 83 columns (with nearly 7000 rows). The first three columns are the same for each file while the remaining 80 are values that... (1 Reply)
Discussion started by: kylle345
1 Replies

7. Shell Programming and Scripting

averaging columns

Hi, I have a file that has 201 columns (1 for the name of each row and the other 200 are values) I want to print the average for each column the file looks like this (as an example this only has 7 columns with values) 1 2 3 4 5 6 7 abr 5 6 7 1 2 4 5 hhr 2 1 3 4 ... (8 Replies)
Discussion started by: kylle345
8 Replies

8. Shell Programming and Scripting

Averaging Data From Multiple Columns, Using Header if Possible

Hi, I have a file with multiple tab delimited columns and I would like to have the average of each column: Iteration Tree No Lh HMean 1000 1 -78.834717 -78.834717 1100 1 -77.991031 -78.624046 1200 1 -79.416055 -78.761861 1300 1 -79.280494 -78.968099 1400 1 -82.846275 -80.808696 ... (4 Replies)
Discussion started by: mikey11415
4 Replies

9. UNIX for Dummies Questions & Answers

Averaging 100 columns together line by line

Hi I have 100 xy graphs and need to average these together in a line by line fashion. The value of the x axis are the same. y differs e.g. taking only 2 graphs: graph 1 x y 1 3 2 5 3 7 4 9 5 11 graph 2 x y 1 4 2 6 3 10 (2 Replies)
Discussion started by: jenjen_mt
2 Replies

10. Shell Programming and Scripting

need help with post:extract multiple columns from multiple files

hello, I will would be grateful if anyone can help me reply to my post extract multiple cloumns from multiple files; skip rows and include filenames; awk Please see this thread. Thanks manishabh (0 Replies)
Discussion started by: manishabh
0 Replies
Login or Register to Ask a Question