Awk Error Division By Zero Attempted
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more awk divide by zero about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users
Awk Fatal Division By Zero Attempted
Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping awk (filename=- fnr=1) fatal each other. Join them; it only takes a minute: Sign up Awk, trying NOT to divide by zero up vote 6 down vote favorite I'm trying to divide two fields (where sometimes the divisor might be zero) with awk calculate percentage Awk. I thought this would work: awk -F, '{if ($6 != 0) print $3/$6}'
If Else Awk
solve the problem, but it's more idiomatic awk. –onemasse Oct 26 '11 at 16:00 add a comment| 1 Answer 1 active oldest votes up vote 12 down vote accepted "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. share|improve this answer answered Oct 26 '11 at 15:58 William Pursell 92.4k21151196 1 Yikes! A good reminder that awk, while wonderful for text-munging, was not designed for mathematics. –Eric Wilson Oct 26 '11 at 16:03 Thanks, I had forgot that about Awk as well. –phileas fogg Oct 28 '11 at 19:19 That's a bug in the language or implementation. If a string which does not look like a number, such as bar, behaves like zero under operators like + opposite a numeric operand, it should also behave like zero when compared to a number using relational operators like =. This could work perfectly well, while staying in the Awk paradigm. –Kaz Jun 30 '14 at 20:21 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer,
Search Tutorials/Articles Search HCL Search Reviews Search ISOs Go to Page... LinuxQuestions.org > Forums > Non-*NIX Forums > Programming [SOLVED] [awk] division by awk division NaN User Name Remember Me? Password Programming This forum is for awk if else one line all programming questions. The question does not have to be directly related to Linux and any language is fair game. Notices Welcome to LinuxQuestions.org, a friendly and active Linux Community. You are currently viewing LQ as a guest. By joining our community you will have the ability http://stackoverflow.com/questions/7905317/awk-trying-not-to-divide-by-zero to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today! Note that registered members see fewer ads, and ContentLink is completely disabled once you log in. Are you new to LinuxQuestions.org? Visit the following links: Site Howto | Site http://www.linuxquestions.org/questions/programming-9/%5Bawk%5D-division-by-nan-818052/ FAQ | Sitemap | Register Now If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here. Having a problem logging in? Please visit this page to clear all LQ-related cookies. Introduction to Linux - A Hands on Guide This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter. For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own. Click Here to receive this Complete Guide absolutely free. Search this Thread 07-05-
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the http://unix.stackexchange.com/questions/149589/pipe-line-with-non-deterministic-output company Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to by zero the top pipe line with non-deterministic output up vote 4 down vote favorite 1 I have happened upon a command that sometimes works and sometimes does not, even when executed multiple times in rapid succession in a bash shell (I have not tested the behavior in other shells). The problem has been localized to the reading of a variable in the BEGIN block of an awk statement at the end of the pipe line. During division by zero some executions, the variable is correctly read in the BEGIN block and during other executions, the operation fails. Supposing this aberrant behavior can be reproduced by others (and is not a consequence of some problem with my system), can its inconsistency be explained? Take as input the following file called tmp: cat > tmp <