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 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?
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)