---------- 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
# 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.