Comment rows are counted in the NR.
| sed -e '1d'
, to ignore comment rows?Example
$ awk '{sum+=$3} END {avg=sum/NR} END {print avg}' coriolis_data
0.885491 // WRONG divided by 11, should be by 10
$ cat coriolis_data
#d-err-t-err-d2-err
.105 0.005 0.9766 0.0001 0.595 0.005
.095 0.005 0.9963 0.0001 0.595 0.005
.115 0.005 0.9687 0.0001 0.595 0.005
.105 0.005 0.9693 0.0001 0.595 0.005
.095 0.005 0.9798 0.0001 0.595 0.005
.105 0.005 0.9798 0.0001 0.595 0.005
.095 0.005 0.9711 0.0001 0.595 0.005
.110 0.005 0.9640 0.0001 0.595 0.005
.105 0.005 0.9704 0.0001 0.595 0.005
.090 0.005 0.9644 0.0001 0.595 0.005
it is best not to touch NR
, use a different variable for counting the rows. This version skips comments as well as blank lines.
$ awk '!/^[ \t]*#/&&NF{sum+=$3;++d}END{ave=sum/d;print ave}' file
0.97404
Just decrement NR yourself on comment lines:
awk '/^[[:space:]]*#/ { NR-- } {sum+=$3} END { ... }' coriolis_data
Okay, that did answer the question you asked, but the question you really meant:
awk '{ if ($0 ~ /^[[:space:]]*#/) {NR--} else {sum+=$3} END { ... }' coriolis_data
(It's more awk-ish to use patterns outside the blocks as in the first answer, but to do it that way, you'd have to write your comment pattern twice.)
Edit: Will suggests in the comments using /.../ {NR--; next}
to avoid having the if-else block. My thought is that this looks cleaner when you have more complex actions for the matching records, but doesn't matter too much for something this simple. Take your favorite!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With