print max number of 2 columns - awk

Is it possible to print max number of 2 columns - awk
note: print max if the integer is positive and print min if the integer is negative

input

a  1  2
b  3  4
c  5  1
d  -3  -5
d  -5  -3

output

a  2
b  4
c  5
d  -5
d  -5

What happens when there is one positive and a negative integer in 2nd or 3rd column..?

awk '/-/{print ($2 > $3)? $1 FS $3 : $1 FS $2;next}{print ($2 > $3)? $1 FS $2: $1 FS $3}' inputfile

Try:

awk '{print $1, ($2*$2>$3*$3)?$2:$3}' infile
1 Like

What if its the data is like this.

2nd col is linked with col4
3rd col is linked with col5
After selecting the max positive and min negative from col4 and 5, print the linked column along with it.

For ex:

a  1  0  1  2
b  -1  -2  3  4
c  -3  -4  5  1
d  -3  -4  -3  -5
d  -3  -4  -5  -3
a  0  2
b  -2  4
c  -3  5
d  -4  -5
d  -3  -5

It works the same, try this:

awk '{if($4*$4>$5*$5)print $1,$2,$4; else print $1,$3,$5}' infile