Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Averaging columns 1

Status
Not open for further replies.
Aug 10, 2000
37
GB
Hello all,

I seem to have pushed my awk knowledge to the limit, any help would be greatly appreciated.

I have a file in the following format

14:15:01 47 44 50 43 50 47 46 46 41 55 53 34
14:30:00 49 44 48 40 52 47 41 40 41 51 53 34
14:45:01 47 44 46 43 53 47 42 46 40 55 53 32

I would like to append a line to this with an average of each column. The first field is a timestap so I want to ignore that and average all the other columns. The thing thats really baffling me is that the data has a varying number of columns depending on which system I run it on.

Any suggestions ?

Thanks

Chris
 
Something like this ?
awk '
{for(i=2;i<=NF;++i)t+=$i;print;nf=NF;nr=NR}
END{printf "Average: ";for(i=2;i<=nf;++i)printf t/nr;printf "\n"}
' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV, considering the output of your program, I wonder why you called it "spot on":

[tt]
14:15:01 47 44 50 43 50 47 46 46 41 55 53 34
14:30:00 49 44 48 40 52 47 41 40 41 51 53 34
14:45:01 47 44 46 43 53 47 42 46 40 55 53 32
Average: 47.666744484251.666747434440.666753.66675333.3333
[/tt]

Try this:

Code:
{ for (i=2; i<=NF; i++)
    t[i] += $i
  print
  nf = NF
}
END { print "Averages:"
  for (i=2; i<=nf; i++)
    printf " %3d. %g\n", i, t[i]/NR
}

[tt]
14:15:01 47 44 50 43 50 47 46 46 41 55 53 34
14:30:00 49 44 48 40 52 47 41 40 41 51 53 34
14:45:01 47 44 46 43 53 47 42 46 40 55 53 32
Averages:
2. 47.6667
3. 44
4. 48
5. 42
6. 51.6667
7. 47
8. 43
9. 44
10. 40.6667
11. 53.6667
12. 53
13. 33.3333
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top