Simple awk conditional one-liner

Hello,
I'm looking for an awk one-liner that prints the first two data fields, then contains a conditional where if $3>$4, it prints $3-$4. Otherwise, it prints $3. Example:

Data file:

123,456,999,888
333,222,444,555
654,543,345,888
444,777,333,111

Output:

123,456,111
333,222,444
654,543,345
444,777,222

Thanks very much!

SCRIPTS>awk -F"," '{print $1,$2,(($3-$4)>0?$3-$4:$3)}' OFS="," input_file
2 Likes
 awk -F, '{if($3>$4){v=$3-$4}else{v=$3} print $1,$2,v}' OFS="," inputFile

If solaris, use nawk

--ahamed

1 Like