Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-29-2011, 12:22 PM   #1
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 330

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.

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, 01:36 PM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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.
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 01:48 PM. Reason: as stated.
1 members found this post helpful.
Old 05-29-2011, 08:15 PM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Another alternative would be to simply use your variables as the test to your print:
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.


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 07:38 PM
Limit in number of fields that awk can handle ? chargaff Programming 4 03-03-2011 09:36 AM
Division by 0 error cat555 Linux - Newbie 8 01-22-2009 05:58 PM
file division using C fs11 Programming 46 08-17-2007 06:04 PM
C++ - Division by 0 and 'nan' Nightfox Programming 1 05-03-2006 12:58 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:23 AM.

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