LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-29-2011, 11:22 AM   #1
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL 6.x, 5.x, Fedora 20, Kubuntu 12.04, Solaris 10,8
Posts: 321

Rep: Reputation: 17
how to handle division by zero in awk


Hi, I have wrote a little awk script below.

Sometimes the variables will be 0 and sometimes not, and of course the output may also be 0%...

What is the best method to handle division by zero errors in awk.

Code:
awk '/SwapFree:/ {swapfree = $2}; /SwapTotal:/ {swaptotal = $2}; /SwapCached:/ {swapcached = $2}; {printf "Linux Swap utilisation : %.2f%\n", swapfree /(swaptotal+swapcached)}' /proc/meminfo
awk: (FILENAME=/proc/meminfo FNR=1) fatal: division by zero attempted
 
Old 05-29-2011, 12:36 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
What do you want the script to do if the value is zero? Figure that out, then simply run a test on the variables before the print statement to implement the correction.

Since your denominator is the sum of two variables, you may want to first pipe the sum into a new variable and test that. If that's zero, then you can reset it to a new value, or tell it to print something else, or whatever.


Edit: I just tried actually running your code, and I believe your problem is not quite what you think it is. The way it's written, the printf command attempts to run on every line of the file, and so it errors out before it reaches the lines you want. Move it into the END block so that it only prints after the whole file has been read and all the variables have been set.
Code:
awk '/SwapFree:/ {swapfree = $2}; /SwapTotal:/ {swaptotal = $2}; /SwapCached:/ {swapcached = $2}; END{printf "Linux Swap utilisation : %.2f%\n", swapfree /(swaptotal+swapcached)}' /proc/meminfo

Last edited by David the H.; 05-29-2011 at 12:48 PM. Reason: as stated.
 
1 members found this post helpful.
Old 05-29-2011, 07:15 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,438

Rep: Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879
Another alternative would be to simply use your variables as the test to your print:
Code:
awk '/SwapFree:/ {swapfree = $2}/SwapTotal:/ {swaptotal = $2}/SwapCached:/ {swapcached = $2} swapfree && swaptotal && swapcached {printf "Linux Swap utilisation : %.2f%\n", swapfree /(swaptotal+swapcached);exit}' /proc/meminfo
Also notice the exit I placed in as well. This is because once all the variables have been set it will print a result for all remaining lines of the input.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [awk] division by NaN dhodho Programming 10 10-15-2012 06:38 PM
Limit in number of fields that awk can handle ? chargaff Programming 4 03-03-2011 08:36 AM
Division by 0 error cat555 Linux - Newbie 8 01-22-2009 04:58 PM
file division using C fs11 Programming 46 08-17-2007 05:04 PM
C++ - Division by 0 and 'nan' Nightfox Programming 1 05-03-2006 11:58 AM


All times are GMT -5. The time now is 06:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration