Average of a skipping column of 5 using awk

I want to calculate the average of the 1st column of a text file, skipping lines divisible by 5. As an example, consider the following dataset.

1
2
3
4
5
6
7
8
9
10

      

For the above data, I can calculate the average of the entire column using awk

like

awk '{ sum += $1 } END { if (NR > 0) print sum / NR }' file

      

which prints the result 5.5

.

How can I extend this code to exclude lines divisible by 5 from the middle? In the example above, this will exclude the numbers 5

and from the mean 10

, resulting in a new mean 5

.

+3


source to share


1 answer


Short awk solution:

awk '{ NR%5? s+=$0 : c++ }END{ print s/(NR-c) }' file

      

Output:



5

      


  • NR%5? s+=$0 : c++

    - triple condition: sums up all the values s+=$0

    if the record number is NR

    not divisible by 5

    , else - counts the missing records (to subtract them from the average)
+2


source







All Articles