0

I have a file with a list of numbers, like this

4.90889
4.92681
4.99236
5.29358
5.3115
5.37706
5.9354
5.95332
6.01888

I need to subtract the third number with the first, then procede with the next 3 line block recursively, save the results and average them out. I know how to make an average with awk but I'm lost on the first operation.

Using the numbers above it would be:

4.99236 - 4.90889 = 0.08347
5.37706 - 5.29358 = 0.08348
6.01888 - 5.9354 = 0.08348

(0.08347 + 0.08348 + 0.08348)/3 = 0,0834766666667
2

With a bit of modulo arithmetic:

awk '
  FNR%3 == 1 {last = $0} 
  FNR%3 == 0 {sum += $0 - last; n++} 
  END {print (n > 0 ? sum/n : 0)}
' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.