Calculating average with awk

I need to find the average from a file like:

data => BW:123 M:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0'
data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0'
data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0'
data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0'
data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0'

So far I have used sed to extract only the lines I need, but I'm having trouble grasping awk.

I need to find the average of the 1's and 0's (in my data they are fields 11-309 and they're not just 1's and 0's)

Is there really a ' at the end of the line? That's annoying...

What do you want the output to look like?

It may be helpful to show a few original lines of data,
your rules for inclusion,
and especially how many fields (and where placed) to expect
and desired output

For example:

a 1 2 3 4 5
b 2 3 4 5 6
a 3 4 7 3 3

include only lines that begin with "a"

average numbers in field 2-6, space delimited

output

a 3
a 4

yes there really is a ' at the end of each line, but I think it's fairly easy to get rid of with sed.

I want one average for each line, so like:

.568329
.505435
.740328
.824474
.353856

I know, but if you're using awk, things like sed are usually redundant... And if your file really does have 300 columns, the lines might be too wide for sed to accept on some systems. Anyway:

sed "s/'$//" data | awk '{for(N=6; N<=NF; N++) T[N]+=$N ; MAX=NF } END { for(N=6; N<=MAX; N++) print T[N]/NR }' data
1
0
1
0
0
1
1
1
1
0
0
1
1
0

$

Looks funny, but correct since the lines are all the same :smiley: