print the key with highest value
input
a 10
a 20
a 30
b 2
b 3
b 1
output
a 30
b 3
print the key with highest value
input
a 10
a 20
a 30
b 2
b 3
b 1
output
a 30
b 3
awk '{a[$1]=a[$1]<$2?$2:a[$1]} END {for(i in a) print i,a}'
Thanx. Can you also modify it so that it can print the max if the value is positive and minimum if the value is negative ?
Fr ex:
a -1
a -2
a -3
b 1
b 2
b 3
a -3
b 3
awk '{if($2<0)a[$1]=a[$1]>$2?$2:a[$1];else a[$1]=a[$1]<$2?$2:a[$1]} END {for(i in a) print i,a}'
or
sort -rg infile | awk '!a[$1]++'
Assuming that for a $1 value, all $2 value have the same sign :
awk '{a[$1]=sqrt($2^2)>sqrt(a[$1]^2)?$2:a[$1]}END{for(i in a) print i,a}' infile
So then is -4 higher than 3?
awk '$2*$2>A[$1]*A[$1]{A[$1]=$2} END{for(i in A) print i,A}' infile
@huaihuaiz3: that second solution will probably not work reliably. Just try adding an extra space between field 1 and 2 on one of the lines and see what happens to the sort..
Is it possible to sum the values instead of taking higher value ?
a -1
a -2
a -3
b 1
b 2
b 3
a -6
b 6
awk '{arr[$1]+=$2} END{for (i in arr) {print i, arr } } ' somefile
Consider reading a book like 'sed/awk' or a tutorial on awk. It is an amazing tool.
--deleted--
Other parameters may be referenced by sort depend on actual conditions!