Hi everyone, I'm a new member at the forum
I have a file like this: field 2 values are either 0 or negative. file test4:
100815 -20
118125 0
143616 0
154488 0
154488 0
154488 -6
196492 -5
196492 -9
196492 -7
27332 0
29397 0
I would like to print a line containing the maximum value in field 2 of all occurrences for each value in field 1. So the desired output should be:
100815 -20
118125 0
143616 0
154488 0
196492 -5
27332 0
29397 0
I am using awk to do this. But, there are two problems: first awk prints nothing when I try to get the maximum for the values with negative numbers but it has no problem when I try the same with positive numbers. This made me take the absolute values which I can later turn back to the original values. The second problem, I get the following output with my code:
100815 20
118125 0
143616 0
154488 6
196492 7
27332 0
29397 0
Although with absolute values I should get:
100815 20
118125 0
143616 0
154488 0
196492 5
27332 0
29397 0
My code is:
awk '{$2>0?$2=$2:$2=-$2} $2==0 {$2=0} {print}' test4 | awk 'NR==1 {a[$1]=$2} {a[$1]=$2 ; if ($2<a[$1]) a[$1]=$2; else a[$1]=a[$1];} END {for(i in a) print i"\t"a;}'
I am sure I'm missing something basic and this could probably be done in a much simpler way. Any help is appreciated
Best reagrds to all