awk - sort, then print the high value for each group

Hi @ all

I'm trying to achive to this problem,

I've a 2-column composed file as the following:

192.168.1.2 2
192.168.1.3 12
192.168.1.2 4
192.168.1.4 3
cpc1-swan1-2-3-cust123.swan.cable.ntl.com 4
192.168.1.3 5
192.168.1.2 10
192.168.1.4 8
cpc1-swan1-2-3-cust123.swan.cable.ntl.com 8

and i would this kind of output :

192.168.1.2 10
192.168.1.3 12
192.168.1.4 8
cpc1-swan1-2-3-cust123.swan.cable.ntl.com 8

I'm trying to use this methodology :

awk  '{  a[$NF]=$2; if ( a[$NF] >= max[$NF] ) max[$NF]=a[$NF] } END { for ( item in a )  print item,max[item] }'

but i'm not sure this is the right way....

Thanks you all in advance!!!

m4rco-

nawk '{
  max[$1] = !($1 in max) ? $2 : ($2 > max[$1]) ? $2 : max[$1]
}
END {
  for (i in max)
    print i, max
}' myFile

How about this .....

awk '{print $2" "$1}' file_in_input | sort -k 2 -k 1rn | uniq -f 1 |awk '{print $2" "$1}' >output_file

Hope this is what you needed.
G.

way too many unnecessary 'pipes' for my taste....

Hello,
I am amazed of how smooth the code is ...can you explain a little in words what is the logic of the line:

max[$1] = !($1 in max) ? $2 : ($2 > max[$1]) ? $2 : max[$1]

Thanks.

max[$1] = !($1 in max) ? $2 : ($2 > max[$1]) ? $2 : max[$1]

max[$1] - 'max' is an array indexed by the value of the FIRST field
!($1 in max) ? $2 - if '$1' is NOT already in array 'max', return the value of the SECOND field
: - otherwise
($2 > max[$1]) ? $2 : max[$1]
if the value of the SECOND field is greater than what's already in array 'max' (index by '$1'), return the value of the SECOND field. If not, return the current value of array 'max[$1]'.

'max[$1] =' - whatever gets returned, store it array 'max' index by the FIRST field.

Lil' bit wordy, but I hope you get the gist of it.

wow vgers...
amazing solution, and amazing explaination.

thank u so much!

wow vgersh,

i'm amazed of your great solution and from your clear explaination!

thank you so much!!!!!!!!

m.

awk '{
if($2>_[$1])
        _[$1]=$2
}
END{
        for(i in _)
                print i,_
}' a.txt
#!/usr/bin/perl
open FH,"<a.txt";
while(<FH>){
	chomp;
	my @temp=split(" ",$_);
	$arr{$temp[0]}=$temp[1] if ($temp[1] > $arr{$temp[0]});
}
for $k (keys %arr){
	print $k," ",$arr{$k},"\n";
}