![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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 }
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 }
|
|
#3
|
|||
|
|||
|
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
|
|
#4
|
||||
|
||||
|
Sample input and desired output please.
|
||||
| Google The UNIX and Linux Forums |