Average same line over different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Average same line over different files
# 1  
Old 04-19-2012
Average same line over different files

Hi
Reason tells me that answer to this question should be somewhere on the internet, But I ve not been able to find it.

Here is what I wanna do,
for multiple files like this
file1:
Code:
2 x x x 200
3 x x 400
5 x x 600

file2:
Code:
3 x x 500
6 x x 600
2 x x 500

I want to get an output file which is
Code:
2.5 xx 350
4.5 x x 500
3.5 x x 550

I want to do this over tens of files.
# 2  
Old 04-19-2012
Assuming the same number of columns and lines for each file...
Code:
$ head file[12]
==> file1 <==
2 x x 200
3 x x 400
5 x x 600

==> file2 <==
3 x x 500
6 x x 600
2 x x 500

$ awk '{for(i=1;i<=NF;i++){sum[FNR,i]+=$i;cnt[FNR,i]++}}END{for(j=1;j<=FNR;j++)for(i=1;i<=NF;i++)printf sum[j,i]/cnt[j,i] (i<NF?OFS:ORS)}' file[12]
2.5 0 0 350
4.5 0 0 500
3.5 0 0 550

$

# 3  
Old 04-19-2012
Many thanks Ygor!
Just an extension, if my input is the output of cat folder*/try.txt, then how do I make it run ?

Thanks again.
# 4  
Old 04-19-2012
awk

Code:
paste a b | sed 's/x//g' | awk '{
 a=($1+$3)/2
 b=($2+$4)/2
 print a" x x "b
 }'


Last edited by Scrutinizer; 04-19-2012 at 08:22 AM.. Reason: code tags
# 5  
Old 04-19-2012
@jamie, you cannot use cat, because that means "concatenate" and that would turn all the files into one big file..
# 6  
Old 04-19-2012
Hi
Thanks summer_cherry, simple yet effective Smilie But looks like extending it over large numbers would be trouble.
@scrutinizer - Thanks.!

---------- Post updated at 11:29 AM ---------- Previous update was at 11:18 AM ----------

Just extending the same question, would it be possible to do something like this with gnuplot? Something like plotting all the points in a graph and then fit a line that will be average of group of points? instead of processing the average and then plotting it.

Many thanks.

Last edited by jamie_123; 04-19-2012 at 06:24 AM..
# 7  
Old 04-19-2012
You can supply file names to awk in same way that you would to cat.

I realised that I only need to increment the count per file, so slightly simpler...
Code:
awk 'FNR==1{cnt++}{for(i=1;i<=NF;i++)sum[FNR,i]+=$i}END{for(j=1;j<=FNR;j++)for(i=1;i<=NF;i++)printf sum[j,i]/cnt (i<NF?OFS:ORS)}' folder*/try.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Average of a columns from three files

hello, I have three files in the following order ==> File1 <== 1 20977000 20977000 A C 1.00 0,15 15 45 1 115829313 115829313 G A 0.500 6,7 13 99 ==> File2 <== 1 20977000 20977000 A C 1.00 0,13 13 39 1 115829313 ... (5 Replies)
Discussion started by: nans
5 Replies

2. Shell Programming and Scripting

Calculating average from files

I have some files with the following contents.I would like to calculate average of fifth column. How can I do this with awk? file1 cat 95.9 152 78.0 17.9 rat 67.1 153 36.5 30.6 dog 81.4 154 68.1 13.3 dog 92.0 155 55.5 36.5 rat 73.8 156 23.9 49.9 file2 rat... (4 Replies)
Discussion started by: avina
4 Replies

3. Shell Programming and Scripting

Match first two columns and average third from multiple files

I have the following format of input from multiple files File 1 24.01 -81.01 1.0 24.02 -81.02 5.0 24.03 -81.03 0.0 File 2 24.01 -81.01 2.0 24.02 -81.02 -5.0 24.03 -81.03 10.0 I need to scan through the files and when the first 2 columns match I... (18 Replies)
Discussion started by: ncwxpanther
18 Replies

4. Shell Programming and Scripting

How to take a Average of numbers from different files?

Hi, I have 3 to 4 different files, from that i need to take a Average of numbers from a particular column. here i have to take 4th column, that should present in diff. file. File 1: Col1 col2 col3 col4 1 11 sa 12.00 2 22 sb 134.59 3 33 sc 11.99 4 44 sd 12.44 Col1 col2 col3... (8 Replies)
Discussion started by: Shenbaga.d
8 Replies

5. Shell Programming and Scripting

Average of a column in multiple files

I have several sequential files with name stat.1000, stat.1001....to stat.1020 with a format like this 0.01 1 3822 4.97379915032e-14 4.96982253992e-09 0 0.01 3822 1 4.97379915032e-14 4.96982253992e-09 0 0.01 2 502 0.00993165137406 993.165137406 0 0.01 502 2 0.00993165137406 993.165137406 0... (6 Replies)
Discussion started by: kayak
6 Replies

6. Shell Programming and Scripting

Calculating average of 100 different files of same size

Hey guys..... I have many files (lets say 100 or more) of same size, and I want to create a new output file and calculate the average of first row fifth column in all files and print it in first row of output file, then 2nd row fifth col in all 100 files and print it in 2nd row of output... (1 Reply)
Discussion started by: CAch
1 Replies

7. Shell Programming and Scripting

Average number of each line

Hi, Please, can anyone help me how to get the average number of each line using awk Input File: 60 30 23 66 42 32 48 49 27 33 44 22 88 11 21 73 24 38 66 71 Desired Output: 44.2 37.8 37.2 54.4 Thanks (7 Replies)
Discussion started by: nica
7 Replies

8. Shell Programming and Scripting

Average of elements throught multiple files

Hi, I got a lot of files looking like this: 1 0.5 6 All together there are ard 1'000'000 lines in each of the ard 100 files. I want to build the average for every line, and write the result to a new file. The averaging should start at a specific line, here for example at line... (10 Replies)
Discussion started by: chillmaster
10 Replies

9. HP-UX

calculate average of multiple line data

I have a question as below and i need to write a shell or perl script for this query:My Input file looks like below RNo Marks 12 50 15 70 18 80 12 40 13 55 18 88 13 75... (4 Replies)
Discussion started by: smacherla
4 Replies

10. Shell Programming and Scripting

script to find the average number or files?

Anyone has a script or command in UNIX that can take 4 to five different numbers and calculate the average? (2 Replies)
Discussion started by: bbbngowc
2 Replies
Login or Register to Ask a Question