Division problem -Awk

input

one  two  three  four
0	0	0	10424
0	102	0	15091
1	298	34	11111
0	10	0	1287

scripts

awk 'NR>1{print ($1/$2) / ($3/$4)}'
awk 'NR>1{ if ($1 ||$3 ||$2|| $4 == 0) print 0; else print (($1/$2)/($3/$4))}'

error

awk: division by zero
 input record number 1, file rm
 source line number 1
awk '!$1 || !$2 || !$3 || !$4 {print "0";next}{print (($1/$2)/($3/$4))}' file

I still get the error:

$ awk '!$1 || !$2 || !$3 || !$4 {print "0";next}{print (($1/$2)/($3/$4))}' infile
awk: (FILENAME=infile FNR=1) fatal: division by zero attempted

Try this:

$ awk 'NR>1{print (!$1 || !$2 || !$3 || !$4)? "0":($1/$2)/($3/$4)}' infile

0
0
1.09662
0

While both Franklin52's and rdcwayx's solutions work for me, neither should, according to POSIX.

If i'm not mistaken, the portable way to write

!$1 || !$2 || !$3 || !$4

is

!($1+0) || !($2+0) || !($3+0) || !($4+0)

to explicitly demand that each field variable be treated as a number.

The standard says that in a boolean context (such as the logical not), if the type is a number, 0 is false and all others are true; if the type is a string, the null string is false and all others are true. There is no conversion of type (as there is when comparing, in which case a 'numeric string' is coerced to a number if the other value is a number). So when a field is "0", that's a true value in a boolean context since it's a non-empty string.

I think I'd just go with an explicit comparison to zero, $i == 0, since it's both clear and portable (the hardcoded number 0 ensures that the field value is treated as a number).

For more detailed info, refer to the 'Expressions in awk' section @ awk

Wow, that's a lot of typing for a little division. :wink:

Regards,
Alister

Ok, for Franklin52's command, if I remove the first line from input file, it is fine.

The reason why the problem is fixed in my code, because I add NR>1 , otherwise, same error I will get.

@rdcwayx:

Is it possible to define a range is ==0 for ex:

!$1 to !$4)? "0":

yes, you can.

awk 'NR>1{print ($1=="0" || $2=="0" || $3=="0" || $4=="0")? "0":($1/$2)/($3/$4)}' infile

I mean instead of typing

($1=="0" || $2=="0" || $3=="0" || $4=="0")

is is there any short way of doing it ? Because I have around 300 columns to specify as 0. Thanx

awk 'NR>1{print $2*$3*$4?($1/$2)/($3/$4):0}' infile

@Scrutinizer, a nice one :b: