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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

average part of column

Status
Not open for further replies.

vidmo

Technical User
Sep 18, 2006
14
GB
I know this question has been put before, but in a more simplistic way, like averaging $1 every 5 rows.

I would like to know how to average out a column when $1 has the same number ( but there is not equal amounts of numbers in $1). e.g.
635 -1.47 0 62.125 8.835 7.365 5.5
635 -1.97 1.91877 69.575 8.835 6.865 4.3
635 -2.47 3.91275 70.75 8.835 6.365 8.5
635 -2.97 1.23997 69.175 8.835 5.865 5.6
636 -1.47 0 60.725 8.71 7.24 10.8
636 -1.97 0 64.2 8.71 6.74 15.3
636 -2.47 0.2642 68.6 8.71 6.24 8.5
637 -1.47 0 60.95 8.96 7.49 8.4
637 -1.97 0 62.575 8.96 6.99 5.3
638........
while $1 = 635, average out $7

output:
635 -1.47 0 62.125 8.835 7.365 5.5 5.975 (average printed as $8)
635 -1.97 1.91877 69.575 8.835 6.865 4.3 5.975
635 -2.47 3.91275 70.75 8.835 6.365 8.5 5.975
635 -2.97 1.23997 69.175 8.835 5.865 5.6 5.975
636 -1.47 0 60.725 8.71 7.24 10.8 11.53
636 -1.97 0 64.2 8.71 6.74 15.3 11.53

where i am really struggling is the part of: 'if/while $1 = $1, then do'
because $1 is always going to equal $1!

argh!

thanks

 
this works but if anyone knows how to shrink it, i would be very happy.
Code:
rm -f data_out.txt
minmax -C data_in.txt | awk '{print "min="$1; print "max="$2}' > minmax.var
. minmax.var
for ((i = $min; i <= $max; i++))
do
awk '{if ($1 == "'$i'") print $0}' data_in.txt  > temp.dat
awk '{
   s += $7}
   END{
   print "sum="s; print "lines="NR}' temp.dat > sum.var
. sum.var
awk '{print $0, "'$sum'"/"'$lines'"}' temp.dat >> data_out.txt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top