Good Morning Mike-

I believe this will do it with just awk:

awk 'NR == 1 || $1 < min {min = $0} END{print min}'

SDA

On Thu, Nov 12, 2015 at 2:35 PM, Mike Miller <mbmiller+l at gmail.com> wrote:

> I liked this one because I've always wanted "max" and "min" programs. What
> I usually do is sort and use head or tail.  That is very inefficient. So
> based on your work I made this:
>
> awk 'BEGIN {max=$1} {if ($1>max) max=$1} END {print max}'
>
> It seems to do what I want for whole lines if I do this:
>
> awk 'BEGIN {max=$0} {if ($0>max) max=$0} END {print max}'
>
> It looks like these two lines give the same output for most files:
>
> awk 'BEGIN {max=$0} {if ($0>max) max=$0} END {print max}' filename
> sort filename | tail -n1
>
> Of course the awk code is much faster and I assume it uses much less
> memory, especially for larger input files.
>
> If input is a single column of numbers, awk will provide the numerical
> maximum number, but sort will not do that unless the -g option is used
> (sometimes -n would be enough, but not always).
>
> Any ideas about this?
>
> I'm having a problem getting min to work.  This gives me a blank line as
> output:
>
> echo -e "1\n2" | awk 'BEGIN {min=$1} {if ($1<min) min=$1} END {print min}'
>
> I'm obviously missing something!
>
> Mike
>
>
>
> On Fri, 30 Oct 2015, Saul Alanis wrote:
>
> Thanks Gerry.
>>
>> With your suggestion a solution was found:
>>
>> awk -F"|" 'BEGIN {max = 0} $2 ~ /foo/ {if ($1>max) max=$1} END{print max}'
>>
>> Sincerely,
>> SDA
>>
>>
>> On Fri, Oct 30, 2015 at 7:52 PM, gerry <gsker at skerbitz.org> wrote:
>>
>> Saul,
>>> It sounds like you just want the max for the first field?
>>>   gawk '/foo/{if (max < $1) max=$1} END {print $1}' file
>>>
>>> But that doesn't seem likely since it's too simple - no offence intended.
>>>
>>> Did you want the 2nd field of the row with the max of the first field?
>>>   gawk '/foo/{if (max < $1) {max=$1;save3=$3} } END {print save3;}'
>>>
>>> I'm ignoring the vertical bars because your example had spaces which awk
>>> recognizes by default.
>>> If the spaces are not consistent then you might want to use -F.
>>>
>>> You can probably find a good example of what you want
>>> on http://commandlinefu.com
>>>
>>> --
>>> gsker at skerbitz.org
>>>
>>>
>>> On Fri, 30 Oct 2015, Saul Alanis wrote:
>>>
>>>
>>> I have a file with multiple fields;
>>>>
>>>> 2 | foo | bar
>>>> 4 | bar | foo
>>>> 1 | bar | foo
>>>> 3 | foo | bar
>>>>
>>>> My goal is to sort the first field numerically and print the first field
>>>> of the last result.
>>>>
>>>> awk -F"|" '/foo/ {print $1 | "sort"}'
>>>>
>>>> awk -F"|" '/foo/ {number=$1} END {print version}'
>>>>
>>>> Help is greatly appreciated :)
>>>>
>>>> SDA
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>>
>>> TCLUG Mailing List - Minneapolis/St. Paul, Minnesota
>>> tclug-list at mn-linux.org
>>> http://mailman.mn-linux.org/mailman/listinfo/tclug-list
>>>
>>>
>> _______________________________________________
> TCLUG Mailing List - Minneapolis/St. Paul, Minnesota
> tclug-list at mn-linux.org
> http://mailman.mn-linux.org/mailman/listinfo/tclug-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.mn-linux.org/pipermail/tclug-list/attachments/20151114/ae1fd468/attachment.html>