Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using awk to bin values in a list of numbers

Tags:

awk

I have a large data file consisting of many colums, and i would like to bin the (say) third column and output to a separate file.

By binning i mean the following:

I have a list of numbers say:

1
4
1
1
1
1

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

My final output should be

2
1

The first entry is the average of

1
4
1

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

1
1
1

How do i achieve this using awk?

like image 292
PyariBilli Avatar asked Jun 11 '13 11:06

PyariBilli


1 Answers

Use this awk command:

awk '{sum+=$1} NR%3==0 {print sum/3; sum=0}' inFile
like image 146
anubhava Avatar answered Oct 21 '22 09:10

anubhava