awk fatal:division by zero attempted bypass with a condtion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk fatal:division by zero attempted bypass with a condtion
# 1  
Old 06-12-2013
awk fatal:division by zero attempted bypass with a condtion

Hi Friends,

My input

Code:
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

Code:
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

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

My desired output

Code:
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.
# 2  
Old 06-12-2013
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?

Last edited by Corona688; 06-12-2013 at 12:41 PM..
# 3  
Old 06-12-2013
If your output is incorrect and my program is working:

Code:
$ 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

$

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 06-12-2013
Quote:
Originally Posted by Corona688
If your output is incorrect and my program is working:

Code:
$ 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

$

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.
# 5  
Old 06-12-2013
Try if($5 && ($6>0)) and see if it works.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Fatal division by zero attempted

Hello. I'm writing an awk script that looks at a .csv file and calculates the weighted grade based on the scores and categories in the file. I keep getting a fatal division by zero attempted error and I know what it means but I've been looking over the code for awhile and am not sure what is... (4 Replies)
Discussion started by: Eric7giants
4 Replies

2. Shell Programming and Scripting

Division by zero attempted error during linear conversion of values between 0.25 to 1

I want to implement the below formula with awk oneliner new_value = ((old_value - old_min) / (old_max - old_min) ) * (new_max - new_min) + new_min I want to pass the value of old_min and old_min as variable. Here is what I did for this old_min=$(awk 'BEGIN{a=100000000000}{if ($10<0+a) a=$10}... (2 Replies)
Discussion started by: sammy777888
2 Replies

3. Shell Programming and Scripting

awk Division By Zero Bypass

Hi Friends, I don't understand why "a" is always being printed as zero, when I execute the following command. awk '{if($6||$8||$10||$12==0)a=b=c=d=0;else (a=$5/$6);(b=$7/$8);(c=$9/$10);(d=$11/$12); {print... (6 Replies)
Discussion started by: jacobs.smith
6 Replies

4. Shell Programming and Scripting

Awk: cmd. line:1: fatal: division by zero attempted

when i try the snippet in the console its working fine: ps awwwux | grep php-fpm | grep -v grep | grep -v master | awk '{total_mem = $6 * 1024 + total_mem; total_proc++} END{printf("%d\n", total_mem / total_proc)}' output: but when i try the bash script: #!/bin/sh # -*- sh -*- #... (3 Replies)
Discussion started by: danieloooo
3 Replies

5. Shell Programming and Scripting

awk - Division with condition

Hi Friends, I have an input file like this cat input chr1 100 200 1 2 chr1 120 130 na 1 chr1 140 160 1 na chr1 170 180 na na chr1 190 220 0 0 chr1 220 230 nd 1 chr2 330 400 1 nd chr2 410 450 nd nd chr3 500 700 1 1 I want to calculate the division of 4th and 5th columns. But, if... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. Shell Programming and Scripting

variables division with awk

hello i try to divide 2 variables in order to get a percentage--that's why i'm not interested in integer division--but nothing seems to work I think awk is suitable for this but i'm not quite sure how to use it.. any ideas? here's what I want to do: percentage = varA/varB thank you (2 Replies)
Discussion started by: vlm
2 Replies

7. Shell Programming and Scripting

awk division error - 0

input 0 0 9820373 2069 0 0 11485482 awk '{print ($1/$3) / ($4/$7)}' input error Is there any way to fix this problem ? (25 Replies)
Discussion started by: quincyjones
25 Replies

8. UNIX for Advanced & Expert Users

awk: division by zero

I received error "awk: division by zero" while executing the following statement. SunOS 5.10 Generic_142900-15 sun4us sparc FJSV,GPUZC-M echo 8 | awk 'END {printf ("%d\n",NR/$1 + 0.5);}' file1.lst awk: division by zero Can someone provide solution? Thanks Please use code... (11 Replies)
Discussion started by: kumar77
11 Replies

9. Shell Programming and Scripting

awk error message: division by zero attempted

Hi, I'm executing unixbench tool v4.1 on an embedded system and I'm getting these error messages: Execl Throughput 1 2 3awk: /unixbench/unixbench-4.1.0/pgms/loops.awk:38: (FILENAME=- FNR=4) fatal: division by zero attempted Pipe Throughput 1 2 3 4 5 6 7 8 9 10awk:... (3 Replies)
Discussion started by: rogelio
3 Replies

10. Shell Programming and Scripting

error "awk: (FILENAME=- FNR=23) fatal: division by zero attempted"

Hi , I have file : after i run this command : there are error can we print blank line if output error ?? thanks.. ^^ (4 Replies)
Discussion started by: justbow
4 Replies
Login or Register to Ask a Question