Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk, trying NOT to divide by zero

Tags:

awk

I'm trying to divide two fields (where sometimes the divisor might be zero) with Awk.

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

But it chokes with fatal: division by zero attempted even though I thought the "if" condition took care not to divide if field 6 is zero.

What am I overlooking?

like image 421
phileas fogg Avatar asked Oct 26 '11 15:10

phileas fogg


1 Answers

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

if( $6 + 0 != 0)

instead.

like image 194
William Pursell Avatar answered Nov 14 '22 16:11

William Pursell