awk or Bash: Cumulative average

For the data

I would like to parse down and for each parsing
I want a cumulative averaging, stored in an array
that can be output.

I.e.
546/NR = 546

(546+344)/NR=(546+344)/2 = etc.

For N record input I want N values of the average (a block
averaging effectively)

Any help with this?

Thanks so much!

So what's your logic here? Might I suggest:-

  • Zero total and rows read
  • Read a row
  • Increment row counter
  • Add row value to total
  • Get cumulative mean average by dividing total by rows read
  • Store it in an array based on the rows read
  • Repeat the loop until there is no data left

There are other averages, but I assume that this is the one you want.

So that leaves a few questions?

  • What have you tried so far?
  • What output/errors/messages do you get?
  • What OS & version are you using?
  • What are your preferred tools?
  • How big an input file have you got (array limits)

Most important, what have you tried so far?

Robin

Try something like this

$ awk '{print $1,(p+=$1)/NR}'  OFS='\t' file
$ cat file
546
344
1312
3234
1221
2322
$ awk '{print $1,(p+=$1)/NR,(str=str?str"+"$1:$1)"/"NR}' OFS='\t' file
546	546	546/1
344	445	546+344/2
1312	734	546+344+1312/3
3234	1359	546+344+1312+3234/4
1221	1331.4	546+344+1312+3234+1221/5
2322	1496.5	546+344+1312+3234+1221+2322/6
1 Like

Try also

awk '{print $1 "\t" (S+=$1)/NR}' file