The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to sort a field in a file having date values risshanth Shell Programming and Scripting 4 06-04-2008 02:03 AM
read from a file and calculate values for a specified field lucho_1 Shell Programming and Scripting 3 03-11-2008 04:24 PM
Need help with switching field/column values sonyd8 Shell Programming and Scripting 7 02-12-2008 10:10 PM
Search list of values rahulrathod Shell Programming and Scripting 1 12-15-2005 01:27 AM
Korn Shell Script - Read File & Search On Values run_unx_novice Shell Programming and Scripting 2 06-15-2005 04:20 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-28-2008
Registered User
 

Join Date: Sep 2007
Posts: 5
Awk search for max and min field values

hi, i have an awk script and I managed to figure out how to search the max value but Im having difficulty in searching for the min field value.


BEGIN {FS=","; max=0}
NF == 7 {if (max < $6) max = $6;}
END { print man, min}

where $6 is the column of a field separated by a comma
Reply With Quote
Forum Sponsor
  #2  
Old 07-28-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
What's the range of possible values? Can you just initialize min to some ridiculously large value?

Code:
BEGIN { FS=","; max=0; min=9876543210; }
NF == 7 { if (max < $6) max=$6; if (min > $6) min=$6; }
END { print min, max }
Some variants of awk might have a problem with an integer of that size; if you get an error message, try with different values.

Another approach might be to initialize it to an empty string, and make a more complex condition to cover that case. Yet another approach might be to have a sentinel variable which is set to 0 initially and then to 1 when max and min are first defined:

Code:
BEGIN { FS=","; defined=0; }
NF==7 { if (! defined) { max = min = $6; defined = 1; next;
    if (max < $6) max=$6; if (min > $6) min=$6; }
END { print min, max }
Reply With Quote
  #3  
Old 07-29-2008
Registered User
 

Join Date: Sep 2007
Posts: 5
hi era, thank you for taking time to respond. actually, the range will include from a whole number to a decimal number. lets say 75000 - 0.005
Reply With Quote
  #4  
Old 07-29-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,931
Sample input and desired output please.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 08:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0