Division problem -Awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Division problem -Awk
# 1  
Old 02-15-2011
Division problem -Awk

input

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

scripts
Code:
awk 'NR>1{print ($1/$2) / ($3/$4)}'

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

error
Code:
awk: division by zero
 input record number 1, file rm
 source line number 1

# 2  
Old 02-15-2011
Code:
awk '!$1 || !$2 || !$3 || !$4 {print "0";next}{print (($1/$2)/($3/$4))}' file

# 3  
Old 02-15-2011
Quote:
Originally Posted by Franklin52
Code:
awk '!$1 || !$2 || !$3 || !$4 {print "0";next}{print (($1/$2)/($3/$4))}' file

I still get the error:

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


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

0
0
1.09662
0

# 4  
Old 02-15-2011
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
Code:
!$1 || !$2 || !$3 || !$4

is
Code:
!($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 @ http://pubs.opengroup.org/onlinepubs...ities/awk.html

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

Regards,
Alister

Last edited by alister; 02-15-2011 at 10:32 PM..
# 5  
Old 02-15-2011
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 addNR>1, otherwise, same error I will get.
# 6  
Old 02-15-2011
@rdcwayx:

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

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

# 7  
Old 02-16-2011
yes, you can.

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. Shell Programming and Scripting

awk & division

vmstat|awk '{print $3}'|tail -1 returns 6250511, but what I need is 24416, which is 6250511 divided by 256. Please advise. Thank you so much (2 Replies)
Discussion started by: Daniel Gate
2 Replies

5. 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

6. 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

7. Shell Programming and Scripting

hash map Illegal division by zero problem

Hi Everyone, a.txt line1;a;33 line1;c;22 line1;b;0 line1;a;55 a.pl #!/usr/bin/perl use strict; use warnings; my @sorted=(); my @tmp; my $FA; my @F; (0 Replies)
Discussion started by: jimmy_y
0 Replies

8. Shell Programming and Scripting

awk Division and modulus

I need to read the file divide 3 column with 2nd and run a modulus of 10 and check whether the remainder is zero or not if not print the entire line. cat filename | awk '{ if ($3 / $2 % 10 != 0) print $0}' Whats wrong with it ? (4 Replies)
Discussion started by: dinjo_jo
4 Replies

9. UNIX for Dummies Questions & Answers

Problem in division

hi I am having two variables namely a=7 & b=8. I have to subtract these two variables. I am using the command c=`expr $a / $b` When I check the value of c, it comes out to be zero. Please help. Regards Rochit (9 Replies)
Discussion started by: rochitsharma
9 Replies

10. Shell Programming and Scripting

division problem

how can i show the value when i divide a number where the dividend is greater then the divisor. for example... 3 divided by 15 ---> let x=3/15 when i do this in the shell environment it gives me an output of 0. please help me. thanks. (3 Replies)
Discussion started by: inquirer
3 Replies
Login or Register to Ask a Question