print average of values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print average of values
# 8  
Old 11-22-2011
Can you paste the inputfile and the exact output you are getting?

--ahamed

---------- Post updated at 11:03 PM ---------- Previous update was at 11:01 PM ----------

Try this...
Code:
awk ' {
        val=$2+0
        sum[$1] += val;
        count[$1]++;
        !max[$1]?max[$1]=val:NULL
        if( max[$1] < val )
            max[$1] = val
    }
    END {
        for( x in sum )
            printf( "%s ave=%.2f  max=%.2f\n", x, sum[x]/count[x], max[x] );
    }
' input_file

--ahamed

Last edited by ahamed101; 11-22-2011 at 04:44 AM..
# 9  
Old 11-22-2011
Yes it is working great. Thanx!!!! One more thing is it possible to modify the script to select highest value if it is positive and lowest if it is negative ?
# 10  
Old 11-22-2011
highest and lowest for each group?

--ahamed
# 11  
Old 11-22-2011
input

Code:
a  1
a  2
b -1
b -2

output
Code:
a  2
b  -2

# 12  
Old 11-22-2011
Try this...
Code:
awk ' {
        val=$2+0
        sum[$1] += val;
        count[$1]++;
        !max[$1]?max[$1]=val:NULL
         val>0?(max[$1]<val?max[$1]=val:NULL):(max[$1]>val?max[$1]=val:NULL)
    }
    END {
        for( x in sum )
            printf( "%s ave=%.2f  max_min=%.2f\n", x, sum[x]/count[x], max[x] );
    }
' input_file

--ahamed
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

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

10. Shell Programming and Scripting

Script to generate Average Values

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... (5 Replies)
Discussion started by: gagan.delouri
5 Replies
Login or Register to Ask a Question