If column Y contains only positive values, the following awk command works fine:
$ echo -e "g1 2\ng1 3\ng2 4\ng2 1\ng3 1" > input_pos.txt
$ cat input_pos.txt
g1 2
g1 3
g2 4
g2 1
g3 1
$ awk '{if(! $1 in a)a[$1]=$2; else if($2 > a[$1])a[$1]=$2} END{for(i in a) print i,a[i]}' input_pos.txt
g1 3
g2 4
g3 1
It works also well as long as there is at least one positive number:
$ echo -e "g1 2\ng1 -3\ng2 4\ng2 1\ng3 1" > input_pos-neg.txt
$ cat input_pos-neg.txt
g1 2
g1 -3
g2 4
g2 1
g3 1
$ awk '{if(! $1 in a)a[$1]=$2; else if($2 > a[$1])a[$1]=$2} END{for(i in a) print i,a[i]}' input_pos-neg.txt
g1 2
g2 4
g3 1
However, it doesn’t work when there are only negative numbers:
$ echo -e "g1 -2\ng1 -3\ng2 -4\ng2 -1\ng3 -1" > input_neg.txt
$ cat input_neg.txt
g1 -2
g1 -3
g2 -4
g2 -1
g3 -1
$ awk '{if(! $1 in a)a[$1]=$2; else if($2 > a[$1])a[$1]=$2} END{for(i in a) print i,a[i]}' input_neg.txt
g1
g2
g3
Idem in this example:
$ echo -e "g1 -2\ng1 -3\ng2 4\ng2 1\ng3 1" > input_neg2.txt
$ cat input_neg2.txt
g1 -2
g1 -3
g2 4
g2 1
g3 1
$ awk '{if(! $1 in a)a[$1]=$2; else if($2 > a[$1])a[$1]=$2} END{for(i in a) print i,a[i]}' input_neg2.txt
g1
g2 4
g3 1
I looked at the gawk manual (Conversions of strings and numbers), and I tried to add a +0 to $2 to force the > comparison to be performed as numeric, but still can’t find a solution to my problem. Any idea is welcomed!
Your problem is that the
!operator ties harder thanin, thus if you parenthesize(! $1 in a), i.e.(! ($1 in a))it works.