LinuxQuestions.org
Help answer threads with 0 replies.
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-18-2012, 08:38 PM   #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
awk else if statement


I am trying to avoid divide by zero errors and have modified the folowing so that it will account for that, and not print an error.

However something is not correct with the 1-liner's awk syntax. I've tried many variations..

Code:
free -mt | awk '/Swap/ {(if $2 == 0) {print "swap used in % : 0"} else if ($2 != 0) {printf "swap used in % : %.2f\n", $3 / ($2 / 100)}'
 
Old 05-18-2012, 09:40 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
The first ( is on the wrong side of the if statement.

You do a superfluous comparison in the first else case: you already know $2 cannot be zero, so why would you test it again?

(While you use the print and printf statements correctly, there is a high risk of confusion or error. The former prints all its arguments (similar to echo in shell scripts), the latter takes a pattern and optional arguments referred to in the pattern. I prefer to put the parameters to printf in parentheses to emphasize the distinction. Besides, it looks more like the C function then, which is useful, because it works pretty much exactly like the C version.)

You are missing a } at the end.

Try this approach instead:

If the second field is greater than zero, calculate the percentage (by multiplying the third field by a hundred, then dividing by the second field). Then, print out p as a percentage. If $2 was zero, then p is unset, and evaluates to zero.
Code:
free -mt | awk '/Swap/ { if ($2 > 0) p = 100.0 * $3 / $2 ; printf("Swap used: %.2f%%\n", p) }'
 
Old 05-19-2012, 05:56 AM   #3
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL 6.x, 5.x, Fedora 20, Kubuntu 12.04, Solaris 10,8
Posts: 321

Original Poster
Rep: Reputation: 17
Thank you, nice methodology.
 
  


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] I need an awk statement that will look for the home directory and comment it out. Thaidog Programming 12 04-17-2012 06:48 AM
[SOLVED] Getting -nan in output of awk statement sparker1970 Linux - Newbie 3 04-06-2012 10:45 AM
[SOLVED] Problem with if statement in awk in AIX sukhdip AIX 3 01-31-2012 02:28 AM
[SOLVED] variable substitution in awk statement emmalg Linux - Software 12 07-02-2009 08:39 AM
how to keep Field Separator in AWK when using a sub statement tmcguinness Programming 4 02-09-2009 02:24 PM


All times are GMT -5. The time now is 08:58 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