Search for the maximum value

  • Follow


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
0
Reply stebia 1/27/2005 3:49:45 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.
0
Reply Ed 1/27/2005 4:01:04 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
0
Reply Stephane 1/27/2005 4:03:21 PM

Stephane CHAZELAS wrote:
> 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 >=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

0
Reply stebia 2/1/2005 11:59:53 AM

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

Stefano

0
Reply stebia 2/1/2005 12:08:52 PM

4 Replies
158 Views

(page loaded in 0.069 seconds)

Similiar Articles:













7/12/2012 2:50:55 PM


Reply: