For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2005 > Search for the maximum value









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Search for the maximum value
Stefano Bianchi

2005-01-27, 3:56 pm

Hi all,

I have a file like this one:

SOURCE RADIUS MAX_SN MAX_CR
200.00000 15.788781 0.43000000
220.00000 16.200239 0.43000000
240.00000 16.662761 0.46000000
260.00000 16.896194 0.46000000
280.00000 17.149818 0.32000000
300.00000 17.316400 0.32000000
.....
.....

I simply want to find out the maximum value for MAX_SN and then store
the three values in that row into three variables to use in my script.
So, in this case 17.316400 is the maximum, and I would like to have
$sr, $msn and $mcr pointing to 300.00000, 17.316400 and 0.32000000.

I have found a solution, but it's a little bit cumbersome.

I'm sure awk can do much better!

Thanks,

Stefano
Stephane CHAZELAS

2005-01-27, 3:56 pm

2005-01-27, 07:49(-08), Stefano Bianchi:
[...]
> SOURCE RADIUS MAX_SN MAX_CR
> 200.00000 15.788781 0.43000000
> 220.00000 16.200239 0.43000000
> 240.00000 16.662761 0.46000000
> 260.00000 16.896194 0.46000000
> 280.00000 17.149818 0.32000000
> 300.00000 17.316400 0.32000000
> ....
> ....
>
> I simply want to find out the maximum value for MAX_SN and then store
> the three values in that row into three variables to use in my script.
> So, in this case 17.316400 is the maximum, and I would like to have
> $sr, $msn and $mcr pointing to 300.00000, 17.316400 and 0.32000000.
>
> I have found a solution, but it's a little bit cumbersome.

[...]

If you mean $sr... are to be POSIX shell variables,

#! /bin/sh -
eval "(
< file.txt awk '
NR > 1 && $2 >= msn {
msn = $2
sr = $1
mcr = $3
}
END {
print "sr=" sr, "msn=" msn, "mcr=" mcr
}'
)"
printf '%s is %s\n' \
msn "$msn" \
sr "$sr" \
mcr "$mcr"

# but be careful that the values must not contain any shell
# special characters (like blanks or quotes or &, ;...) as they
# are passed to "eval"

--
Stéphane
Ed Morton

2005-01-27, 3:56 pm



Stefano Bianchi wrote:

> Hi all,
>
> I have a file like this one:
>
> SOURCE RADIUS MAX_SN MAX_CR
> 200.00000 15.788781 0.43000000
> 220.00000 16.200239 0.43000000
> 240.00000 16.662761 0.46000000
> 260.00000 16.896194 0.46000000
> 280.00000 17.149818 0.32000000
> 300.00000 17.316400 0.32000000
> ....
> ....
>
> I simply want to find out the maximum value for MAX_SN and then store
> the three values in that row into three variables to use in my script.
> So, in this case 17.316400 is the maximum, and I would like to have
> $sr, $msn and $mcr pointing to 300.00000, 17.316400 and 0.32000000.


Here's how to print the line with the max in the middle column:

gawk 'NR==1{next}$2>p{p=$2;m=$0}END{print m}'

The "NR==1{next}" is to skip your header line so if you deon't really
have one you can delete that part.

You talk about wanting "$sr", etc. to be populated. I don't know what
you mean by that but it sounds like you may want to interface the above
with a shell script. You can do that, or you can just write the above
awk script trivially with a shell loop too. Follow up in comp.unix.shell
if you're interested.

Ed.
stebia@inwind.it

2005-02-01, 8:56 am


Stephane CHAZELAS wrote:
> 2005-01-27, 07:49(-08), Stefano Bianchi:
> [...]
store[color=darkred]
script.[color=darkred]
> [...]
>
> If you mean $sr... are to be POSIX shell variables,
>
> #! /bin/sh -
> eval "(
> < file.txt awk '
> NR > 1 && $2 >=3D msn {
> msn =3D $2
> sr =3D $1
> mcr =3D $3
> }
> END {
> print "sr=3D" sr, "msn=3D" msn, "mcr=3D" mcr
> }'
> )"
> printf '%s is %s\n' \
> msn "$msn" \
> sr "$sr" \
> mcr "$mcr"
>
> # but be careful that the values must not contain any shell
> # special characters (like blanks or quotes or &, ;...) as they
> # are passed to "eval"
>=20
> --=20
> St=E9phane


stebia

2005-02-01, 8:56 am

Thank you guys!
I've implemented your solutions in a shell script and everything works
as I desired.

Stefano

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com