Using awk for bin values ​​in a list of numbers

I have a large data file consisting of many columns, and I would like to put (say) a third column and output it to a separate file.

As a result of binning, I mean the following:

I have a list of numbers:

1 4 1 1 1 1 

I want the average number of sets (say) of three consecutive numbers.

My end result should be

 2 1 

The first entry is the average

 1 4 1 

And the second entry is the average of the next three numbers,

 1 1 1 

How to achieve this with awk?

+4
source share
1 answer

Use this awk command:

 awk '{sum+=$1} NR%3==0 {print sum/3; sum=0}' inFile 
+8
source

Source: https://habr.com/ru/post/1485607/


All Articles