LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 10-17-2012, 10:41 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
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.
 
Old 10-17-2012, 10:56 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,456

Rep: Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844
Code:
prctg=`echo | awk '{print 100 - $var2\*100/$var1}'`
  1. awk doesn't see bash variables
  2. you shouldn't escape * in awk code (it's already quoted for bash by the single quotes)
  3. 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.
Old 10-17-2012, 02:51 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
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!!
 
Old 10-17-2012, 03:47 PM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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)
 
Old 10-17-2012, 11:21 PM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Quote:
Originally Posted by Snark1994 View Post
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!!
 
Old 10-18-2012, 02:26 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,456

Rep: Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844Reputation: 844
Quote:
Originally Posted by meninvenus View Post

echo | awk "BEGIN{print 100 - $var2*100/$var1}"
The echo | should not be necessary when using BEGIN.
 
Old 10-20-2012, 10:17 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.
  


Reply

Tags
awk


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] Need solution for awk: syntax error near line 1 ERROR Vthimmap Linux - Newbie 5 10-16-2012 04:04 AM
awk error awk: line 2: missing } near end of file boscop Linux - Networking 2 04-08-2012 10:49 AM
awk error windstory Linux - Newbie 9 02-22-2012 06:01 PM
bash script read error and awk ouptut error whited Programming 4 10-16-2007 07:05 PM
AWK Fatal Error taintedazure Mandriva 1 09-23-2003 09:11 PM


All times are GMT -5. The time now is 06:14 PM.

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