Partial average of a column with awk

Hello,

Let's assume I have 100 files FILE_${m} (0<m<101). Each of them contains 100 lines and 10 columns.
I'd like to get in a file called "result" the average value of column 3, ONLY between lines 11 and 17, in order to plot that average as a function of the parameter m.
So far I can compute with awk the average of the full column 3 (dividing by NR), but I can't get only the 7 lines that I want... any idea?
Here is my try:

#!/bin/bash
    echo "# m    AVERAGE" > result
    for ((m=1; m<=100; m=m+1))
    do
        FILE=FILE_${m}
        AVERAGE=$(awk '{for(NR=11; NR<= 17; NR++){sum +=$3};} END {print sum/7}' $FILE)                                 
        echo "$m  $AVERAGE" >> result                    
    done

Thanks in advance! :slight_smile:

AVERAGE=$(awk ' NR==11 && NR<= 17{sum +=$3; if(NR == 17){exit}} END {print sum/7}' $FILE) 

Thanks anbu23! I just have one question:
I tried with only one FILE_${m} containing 1.0 in each line on column 3.
So the average should be 7*1.0/7 = 1.0, but instead I get 0.142857 which is equal to 1/7.
So I replaced your solution with:
AVERAGE=$(awk ' NR==11 && NR<= 17{sum +=$3; if(NR == 17){exit}} END {print sum}' $FILE)

This time it works, but I don't understand why... any idea?

NR==11 && NR<= 17

This is only true if NR==11, try:

AVERAGE=$(awk 'NR==11,NR==17{sum+=$3}NR==17{exit}END{print sum/7}' $FILE)

Use Franklin52's code or this

AVERAGE=$(awk ' NR>=11 && NR<= 17{sum +=$3; if(NR == 17){exit}} END {print sum/7}' $FILE)

Thanks!
I do not fully understand the use of the coma between NR==11 and NR==17, just by curiosity, how would you increment by 2 for example? (to sum on even lines for example)

It selects records in the range between the 2 conditions.

This uses the modulo operator % to select the even lines:

awk '!(NR%2){sum+=$3;i++}END{print sum/i}' $FILE