Awk, trying not to divide by zero

I am trying to separate two fields (where sometimes the divisor can be zero) with Awk.

I thought this would work: awk -F, '{if ($6 != 0) print $3/$6}' <some file>

But he suffocates with fatal: division by zero attempted , although I thought that the "if" condition was taken care not to be divided if field 6 is zero.

What am I missing?

+8
awk
source share
1 answer

"bar"! = 0. If $ 6 is a string, the comparison is not performed, but when converted to a number for division, it is zero. Use

 if ($ 6 + 0! = 0)

instead.

+15
source share

All Articles