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

getting average of one field if it matches another field

Status
Not open for further replies.

starlite79

Technical User
Aug 15, 2008
89
US
Hi again.

I have a very large file (over 22000 records) for which I want to search for a field equaling 5.0, 6.0 and 7.0 (third field) and getting the count and average of the second field matching those 3 values. My code runs, but I don't trust the values I get. Could someone offer some help?

Code:
$3 == 5.0 { n = n + 1; cergares = cergares + $2 }
END	{ if ( n > 0 )
	     print " average residual for", n, "CERGA is", cergares/n
          else 
             print "No CERGA data found"
         }



$3 == 6.0 { n = n + 1; mlrs2res = mlrs2res + $2 }
END	{ if ( n > 0 )
	     print " average residual for", n, "MLRS2 is", mlrs2res/n
          else 
             print "No MLRS2 data found"
         }


$3 == 7.0 { n = n + 1; apacheres = apacheres + $2 }
END	{ if ( n > 0 )
	     print " average residual for", n, "APACHE is", apacheres/n
          else 
             print "No APACHE data found"
        }

My output to screen looks like this:
average residual for 88 CERGA is -4.17045e-06
average residual for 88 MLRS2 is 5.92045e-06
average residual for 88 APACHE is -0.002412

I highly doubt that each of the three techniques have the same number of points (88), so I know something is not right.

 
I used different variables for each counter, and this seems to have solved my problem.

I used n, a, and b.
 
Just so you know for the future... the END {} clauses will all be processed when all input files have been processed, not sequentially like you have them placed in your script, which is why your 'n' variable probably appeared to have been clobbered.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top