minimum number of unique key

input

a    1
a    2
a    -1
b    1
b    2
b    3

output

a    -1
b    1

Thanx

---------- Post updated at 09:42 PM ---------- Previous update was at 09:10 PM ----------

Ok I managed it

pc290 ~/Desktop
$ awk ' NR {if(a[$1]=="") a[$1]=$2;if(a[$1]>=$2) {a[$1]=$2; sa[$1]=$0;}}NR&&$NR>x{x=$NR;line=$0}END{for(i in sa){print sa};}' input
a       -1
b       1

Or like this:

awk '{a[$1]=($2<a[$1]||!a[$1])?$2:a[$1]}END{for(i in a) print i,a}' file
awk '{if(a[$1]==""||a[$1]>=$2)a[$1]=$2}END{for (i in a)print i "\t" a}' infile 

nice ones. I have to agree my code is ugly compare to your codes.

AwkHails

sort -k1,2r -n urfile |awk '{a[$1]=$2} END {for (i in a) print i,a}'

could any one explain ripat code plz
thanx

Sure:

# execute this block for every line
{
    # we will work on a array which will have $1 as key and $2
    # as value only if that $2 is lower than previous a[$1] value.
    # ternary conditional: <condition> ? <value if true> : <value if false>
    # if $2 is less than the current value of a[$1] or if a[$1] 
    # doesn't exist, then we assign $2 to it else
    # we give its own value (ie no change))
    a[$1] = ($2<a[$1]||!a[$1]) ? $2 : a[$1]
}

# once at the end of the file, execute this block
END{
    # traverse array a and print its key and value
    for(i in a) print i,a
}

It is exactly the same idea as with the other awk solutions. It is only written differently.

Thanx. Really appreciate for your time