awk fatal:division by zero attempted bypass with a condtion

Hi Friends,

My input

chr1 100 200 1234E-02 0.01 0.05 10
chr1 100 200 14E-11 0.11 0.50 1
chr1 100 200 134E-22 0.00 0.65 111

My command

awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$13}' input | awk '{v=($5/$6); print $0"\t"v}' OFS="\t" |  awk '{$8=(log($8)/log(2)); print $0}' OFS="\t"

My error

awk: (FILENAME=- FNR=3) fatal: division by zero attempted

My desired output

chr1 100 200 1234E-02 0.01 0.05 10 -1.6094
chr1 100 200 14E-11 0.11 0.50 1 -1.5143
chr1 100 200 134E-22 0.00 0.65 111 0

Usually, the third row is not being printed when I use the above command. But, I would like to know a way to still print the third row on a condition that if 5th column or 6th column is zero, I don't want the command to calculate the log2(), instead print just 0.

Thanks.

Your code doesn't work at all. Your input has no column 13.

Splitting it into three awks like that has made it really hard to tell which terms you're using for what. I cannot make a program that generates the exact numbers you want; your original does not work at all for me, even for the valid ones, and the one I made generates very different numbers from what you want.

Perhaps if you explained what you did want, instead of asking me to reverse engineer a broken program which doesn't do what you want?

If your output is incorrect and my program is working:

$ awk '{ R=0; if($5) R=log($5/$6)/log(2); print $1,$2,$3,$4,$5,$6,$7,R }' OFS="\t" input
chr1    100     200     1234E-02        0.01    0.05    10      -2.32193
chr1    100     200     14E-11  0.11    0.50    1       -2.18442
chr1    100     200     134E-22 0.00    0.65    111     0

$
1 Like

Thanks Corona688. You were right. My input was incorrect. Sorry for it. But, your code did work. I would also like to include the condition that if $6 is zero, do the same.

Try if($5 && ($6>0)) and see if it works.

1 Like