Simple AWK script problem.

Hi all,

I have set up a simple awk script to calculate the average of values that are printed out a number of times per second (the number of time the printing occurs varies). The data is of the format shown below:

1 4.43
1 3.65
1 2.45
2 7.65
2 8.23
2 5.65
3 4.65
3 6.21
.. ..
120 4.65
120 3.66
120 8.55

Where the leftmost column is the second that the data printed out and the rightmost column contains the value. What I would like to do is to for each second, find the average of the values for that second and print out the result, then move on to the next second.

I only started using AWK a day or so again and have been trying to get my head around this. The idea behind the script below is, the sum will be calculated until no more data exists for that second, at which point the average is calculated and printed. The script will then move on to calculate the values for the next second. I have tried everything, but cannot see to get it to work.

Any help would be very much appreciated. :slight_smile:

Patrick.

AWK 
BEGIN{}
{

for (i=1; i<=120; i++){

if($1 = i){
sum+=$2
count++
       }
if($1 != i){
avg = sum/count
print (i " " avg)
avg = 0
sum = 0
count = 0
break
       }
                    }

}
END{ }
 

Almost there ...

awk '$1!=s && c { print s, sum/c; sum=c=0 } 
                { s=$1;  c++; sum+=$2  }
       END { print s, sum/c }'    filename

Output:

1 3.51
2 7.17667
3 5.43
120 5.62

Use printf for pretty printing.

Rubin, works perfectly. I really appreciate your help.

Thanks.
Patrick.

Another easier way is to use the associative arrays:

code:-

awk '
{a[$1]+=$2 ; b[$1]++}
END {for (i in a) {print i,a/b}}
' input_file | sort -n +0

Best Regards

awk '{arr[$1]++;brr[$1]+=$2}END{for (i in brr)print i" "brr/arr}' yourfile