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!

FIND MIN AND MAX OF AN ARRAY

Status
Not open for further replies.

keusch

Technical User
Jun 17, 2001
41
US
How do I find the minumum and maximum of $5 (x) and $6 (y)
Then round xmin, ymin down to the next lowest 1000 {newxmin, newymin}
And round xmax, ymax up to the next 1000 {newxmax, newymax}

write newxmin newxmax newymin newymax
then write the rest of the file (already scripted).

open new file
write prior newxmin newxmax newymin newymax
write rest of file (already scripted)

I haven't been able to find anything on min and max
Thanks
 
Since AWK reads each line of the file and does processing on it, you would need to do something like this:
Code:
BEGIN { xmin = 20000; xmax = 20000; ymin = 20000; ymax = 20000 }
# where 20000 is an average value
{
 if ( $5 < xmin ) { xmin = $5 }
 if ( $5 < xmax ) { xmax = $5 }
 if ( $6 < ymin ) { ymin = $6 }
 if ( $6 < ymax ) { ymax = $6 }
}
END {
 newxmin = (xmin % 1000) * 1000
 newxmax = ((xmax % 1000) + 1 ) * 1000
 newymin = (ymin % 1000) * 1000
 newymax = ((ymax % 1000) + 1 ) * 1000

 print newxmin newxmax newymin newymax
}
This will do I think what you are looking for. I'm sure someone smarter than me can see a hole in my logic. Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
That works (with a few plus minus corrections); however, I need to get the average number from the same file that computes the xmin xmax variables. I'm having trouble at that point. I don't know how to make the script read from the top of the input file once it's calculated the average numbers and to use those average numbers in calcuating the min max numbers.
---
I've included a sample of the input file below (it's listed right after the attempted awk script). Final product should be the min an max of $4 (-109.xxx) and min and max of $5 (39.xx) that can be read through another script. This is what I've done so far using your 1st suggestion.
Can you help???????
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxawk script
awk '
# create average long and lat for min max calculations
BEGIN { print xtot ytot } # begin statement tells me to do this before other statements
{
xtot += $4 #sets up sumation and counter for longitude
++counter
}
{
ytot += $5 #sets up sumation and counter for latitude
++counter
}
/rows selected/ { AC = $1 } #this row actually contains rowcount
END {
{ xavg = xtot/AC }
{ yavg = ytot/AC }

#this is where I need it to read the top of the input file and only include lines with the word TOBIN included ($1 ~ &quot;TOBIN&quot;)
/TOBIN/
BEGIN { xmin = xavg }
{
if ( $4 > xmin ) { xmin = $4 }
}
END {
print xmin } ' township.out > header.lst
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsample of township.out
OBJECT_CLASS SEGMENT SEQ_NO X_POLYGON Y_POLYGON TEXT_STRIN X_TEXT_LABEL Y_TEXT_LABEL
------------------------- ---------- ---------- ---------- ---------- ---------- ------------ ------------
UT391_TOBIN_TOWN 102 1 -109.61769 39.7226868 12S 21E -109.56071 39.766235
UT391_TOBIN_TOWN 102 2 -109.61769 39.7517548
UT391_TOBIN_TOWN 102 3 -109.61752 39.7661743
UT391_TOBIN_TOWN 102 4 -109.61763 39.7806702
4 rows selected.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top