Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| 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 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 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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-17-2012, 10:41 AM
|
#1
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,616
|
Calculation error with awk
I have a script:
#!/bin/bash
var1=1111688
var2=374335
prctg=`echo | awk '{print 100 - $var2\*100/$var1}'`
echo "$prctg"
prctg1=$((echo | awk '{print 100 - $var2\*100/$var1}'))
echo "$prctg1"
But when I invoke it, it's giving me following error:
awk: syntax error near line 1
awk: illegal statement near line 1
testscript.sh: syntax error at line 6: `prctg1=$' unexpected
Could anybody rectify, where I am making mistake? Running it on Solaris 10.
Last edited by shivaa; 10-17-2012 at 10:44 AM.
|
|
|
|
10-17-2012, 10:56 AM
|
#2
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,017
|
Code:
prctg=`echo | awk '{print 100 - $var2\*100/$var1}'`
- awk doesn't see bash variables
- you shouldn't escape * in awk code (it's already quoted for bash by the single quotes)
- you can use the BEGIN pattern instead of the echo | awk ... kludge
Code:
prctg=`awk -vvar1="$var1" -vvar2="$var2" 'BEGIN{print 100 - var2*100/var1}'`
# or, if you are doing just arithmetic in awk, bash's string interpolation could work:
prctg=`awk "BEGIN{print 100 - $var2*100/$var1}"`
Code:
prctg1=$((echo | awk '{print 100 - $var2\*100/$var1}'))
This makes no sense, you are mixing bash's arithmetic evaluation with external commands, maybe you meant $(), ie single parens?
Code:
# use $() instead of ``
prctg1=$(awk -vvar1="$var1" -vvar2="$var2" 'BEGIN{print 100 - var2*100/var1}')
# use $(()) instead of awk
prctg1=$((100 - var2*100/var1))
Quote:
|
Running it on Solaris 10.
|
The bash and awk versions in use are probably more relevant.
|
|
|
1 members found this post helpful.
|
10-17-2012, 02:51 PM
|
#3
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,616
Original Poster
|
Ntubski, thanks for your reply. But unfortunately none of your suggested commands worked! I am running it in Solaris 10 and I am running this script with #!/bin/bash. Bash version installed in my system is 3.00.16(1). If I am using following expression in my script:
prctg=`awk "BEGIN{print 100 - $var2*100/$var1}"`
It gives the error:
awk: divided by zero
Else, all other commands give the error I already mentioned.
Even if I change the shell to #!/bin/csh or #!/bin/tcsh, and set variables using "set <var-name>=<value>", it's still giving that same error as mentioned previously!! 
|
|
|
|
10-17-2012, 03:47 PM
|
#4
|
|
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,624
|
Can we see the whole code as it stands? If I run
Code:
$ var1=1111688
$ var2=374335
$ echo `awk "BEGIN{print 100 - $var2*100/$var1}"`
66.3273
it works fine, as you see. The only division you're doing is by $var1, so I assume the error is there (or in a line of your code you haven't posted)
|
|
|
|
10-17-2012, 11:21 PM
|
#5
|
|
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,616
Original Poster
|
Quote:
Originally Posted by Snark1994
Can we see the whole code as it stands? If I run
Code:
$ var1=1111688
$ var2=374335
$ echo `awk "BEGIN{print 100 - $var2*100/$var1}"`
66.3273
it works fine, as you see. The only division you're doing is by $var1, so I assume the error is there (or in a line of your code you haven't posted)
|
Yes Snark, it finally worked. Perhaps the awk code I was trying previously was wrong. I created a simple script in same environment, tested it, and it worked:
#!/bin/bash
var1=1111688
var2=374335
echo | awk "BEGIN{print 100 - $var2*100/$var1}"
Output: 66.3273
Thanks everyone for your responses!!
|
|
|
|
10-18-2012, 02:26 AM
|
#6
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,017
|
Quote:
Originally Posted by meninvenus
echo | awk "BEGIN{print 100 - $var2*100/$var1}"
|
The echo | should not be necessary when using BEGIN.
|
|
|
|
10-20-2012, 10:17 AM
|
#7
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
Please use *** [code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
awk operates on values fed to it through stdin or files. It doesn't do stand-alone operations except inside the BEGIN part of the statement.
Code:
echo "$var1 $var2" | awk '{ print 100 - $2 * 100 / $1 }'
When doing floating point operations, I recommend using bc instead. It takes the entire expression from stdin and performs it.
Code:
echo "100 - $var2 * 100 / $var1" | bc
See the link I just posted over in your other thread for details.
By the way, Be careful about precedence order in mathematical expressions. These two commands will give different values:
Code:
echo "(100 - $var2) * 100 / $var1" | bc
echo "100 - ($var2 * 100) / $var1" | bc
And finally, $(..) is highly recommended over `..`.
|
|
|
1 members found this post helpful.
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:48 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|