awk to find the avg of every 3 rows but only show last result?

Hi,

I've got as far as this:

awk '{sum+=$1}(NR%3==1){avg=sum/3; print avg}' input.txt

Input it:

0.1
txt
txt
0.2
txt
txt
0.3
txt
txt

So, the I get the results:

0.0333333
0.133333
0.2

It's the last line (0.2) I'm interested in, basically it's the timings from cUrl I'm trying to average. Ideally I'd want to have to awk keep running for n amount of lines, if I need to up the re-running of the check.

How can I get it just to print the last result, or to pull the last result as a variable into my next if this do that nugget?

Thanks in advance, I think I broke google looking for the answer, sorry if I've slowed down your searches today (and yesterday).

Try

awk '!(NR-1)%3 {sum=0} {sum+=$1} END {print sum/3}' file
0.2

EDIT: small but important correction:

awk '!((NR-1)%3) {sum=0} {sum+=$1} END {print sum/3}' file
0.1

Thanks,

I think I'd gone AWK-blind, I can see what's happening now, tell it the lines you're interested in, sum those lines, END to get a total? Then divide by 3.

Thanks again.

END is a special code section which runs only after all input files have finished reading.

My input file is now:

0.10
200
txt
0.20
200
txt
0.30
200
txt

How can I skip out the 200's? I currently get 200.20 as the result?

Show us the code you're using now.

I'm afraid that title plus specification in post #1 are misleading, at least for me who is not a native speaker. Trying to infer the correct meaning, I'd propose

awk '!((NR-1)%3) {sum+=$1} END {print sum/3}' file

try, for skip 200

 LANG=C awk -v sum=0 '{NR%3}{if($1<200){sum+=$1}}END{print sum/3}' file

I have understood: average of every 3rd line, starting with line 1

awk 'NR%3==1 {s+=$1; ++c} END {print s/c}'