Script to generate Average Values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to generate Average Values
# 1  
Old 05-02-2006
Script to generate Average Values

Code:
11.60  12.35  12.49
12.99  13.09  12.91
13.48  14.16  14.18
13.23  13.17  13.15
13.71  13.35  13.12
13.00  12.79  12.83
12.54  12.89  12.89
12.47  12.94  13.00
13.41  13.09  13.09
12.09  12.76  12.93
13.67  13.14  13.01
12.39  12.78  12.86
12.61  12.50  12.11
12.98  12.90  12.77
12.56  12.32  12.39
12.45  12.66  12.66
11.84  12.42  12.54
13.52  13.08  12.96
7.17  9.22  10.76
13.18  12.83  12.23
12.85  12.70  12.66
12.27  12.82  12.80
12.47  12.63  12.64
12.80  12.77  12.80

I have a script which generates the above output.
Now I want a column wise average of these values.So in all I am expecting 3 averages meaning one for each coulum.
Can anyone help me making a script which delivers the above desired output.

Last edited by radoulov; 05-15-2012 at 06:08 AM..
# 2  
Old 05-02-2006
awk -f average.awk file

average.awk:
Code:
BEGIN {
  FS=" "
  count=0
  col1=0
  col2=0
  col3=0
}

{
  count+=1
  col1+=$1
  col2+=$2
  col3+=$3
}

END {
  print col1/count, col2/count, col3/count
}

I think that works...

Last edited by Glenn Arndt; 05-02-2006 at 05:31 PM.. Reason: Sorry, just now noticed that you didn't specify awk. This is awk.
# 3  
Old 05-02-2006
nawk -f gagan.awk myFile

gagan.awk:
Code:
FNR==1 { nf=NF}
{
 for(i=1; i<=NF; i++)
   arr[i]+=$i
  fnr=FNR
}
END {
  for( i=1; i<=nf; i++)
   printf("%.2f%s", arr[i] / fnr, (i==nf) ? "\n" : FS)
}

# 4  
Old 05-03-2006
Nice!!, just one question , why you initialized the variable FNR to 1??

Regards
# 5  
Old 05-03-2006
not initialising

Just checking if FNR==1 and setting nf.
# 6  
Old 05-03-2006
Quote:
Originally Posted by ranj@chn
FNR==1
Opss! Double "=" ... my mistake Smilie
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Calculate average from a given set of keys and values

Hello, I am writing a script which expects as its input a hash with student names as the keys and marks as the values. The script then returns array of average marks for student scored 60-70, 70-80, and over 90. Output expected 50-70 1 70-90 3 over 90 0 The test script so far... (4 Replies)
Discussion started by: nans
4 Replies

2. Shell Programming and Scripting

Compute average based on field values

Im looking for a way to average the values in field 14 (when field 2 is equal to 2016) and fields 3 and 4 (when field 2 is equal to 2017). Any help is appreciated. 001001 2016 33.22 38.19 48.07 51.75 59.77 67.68 70.86 72.21 66.92 53.67 42.31 40.15 001001 2017 ... (10 Replies)
Discussion started by: ncwxpanther
10 Replies

3. Shell Programming and Scripting

Average values of duplicate rows

I have this file input.txt. I want to take average column-wise for the rows having duplicate gene names. Gene Sample_1 Sample_2 Sample_3 gene_A 2 4 5 gene_B 1 2 3 gene_A 0 5 7 gene_B 4 5 6 gene_A 11 12 13 gene_C 2 3 4 Desired output: gene_A 4.3 7 8.3 gene_B 2.5 3.5 4.5 gene_C 2 3 4... (6 Replies)
Discussion started by: Sanchari
6 Replies

4. Shell Programming and Scripting

Calculate average of top n% of values - UNIX

Hey guys, I have several huge tab delimited files which look like this: a 1 20 a 3 15 a 5 10 b 2 15 b 6 10 c 3 23 what I am interested is to calculate the average of top n% of data in third column. So for example for this file the top 50% values are: 23 20 (Please note that it... (11 Replies)
Discussion started by: @man
11 Replies

5. Shell Programming and Scripting

Get the average from column, and eliminate the duplicate values.

Dear Experts, Kindly help me please, I have a big file where there is duplicate values in col 11 till col 23, every 2 rows appers a new numbers, but in each row there is different coordinates x and y in col 57 till col 74. Please i will like to get a single value and average of the x and y... (8 Replies)
Discussion started by: jiam912
8 Replies

6. Shell Programming and Scripting

Average of columns with values of other column with same name

I have a lot of input files that have the following form: Sample Cq Sample Cq Sample Cq Sample Cq Sample Cq 1WBIN 23.45 1WBIN 23.45 1CVSIN 23.96 1CVSIN 23.14 S1 31.37 1WBIN 23.53 1WBIN 23.53 1CVSIN 23.81 1CVSIN 23.24 S1 31.49 1WBIN 24.55 1WBIN 24.55 1CVSIN 23.86 1CVSIN 23.24 S1 31.74 ... (3 Replies)
Discussion started by: isildur1234
3 Replies

7. Shell Programming and Scripting

Average values in a column based on range

Hi i have data with two columns like below. I want to find average of column values like if the value in column 2 is between 0-250000 the average of column 1 is some xx and average of column2 is ww then if value is 250001-5000000 average of column 1 is yy and average of column 2 is zz. And my... (5 Replies)
Discussion started by: bhargavpbk88
5 Replies

8. Programming

javascript average from from values in radiobuttons?

Hi all, can you please help me with these part of my code, I want to calculate average grade from each module and not sure if need to label each element there, or is there any easier option to do that? appreciate your help... (0 Replies)
Discussion started by: me.
0 Replies

9. Shell Programming and Scripting

print average of values

Is it possible to print the average of 2 nd column based on a key in 1st col input a1X 4 a1X 6 a2_1 10 a2_1 20 a2_1 30 a2_1 30 a2_1 10 output a1X 5 a2_1 20 (11 Replies)
Discussion started by: quincyjones
11 Replies

10. Shell Programming and Scripting

average of distinct values with awk

Hi guys, I am not an expert in shell and I need help with awk command. I have a file with values like 200 1 1 200 7 2 200 6 3 200 5 4 300 3 1 300 7 2 300 6 3 300 4 4 I need resulting file with averages of... (3 Replies)
Discussion started by: saif
3 Replies
Login or Register to Ask a Question