LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Calculation error with awk (http://www.linuxquestions.org/questions/linux-newbie-8/calculation-error-with-awk-4175432706/)

shivaa 10-17-2012 10:41 AM

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.

ntubski 10-17-2012 10:56 AM

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.

shivaa 10-17-2012 02:51 PM

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!! :(

Snark1994 10-17-2012 03:47 PM

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)

shivaa 10-17-2012 11:21 PM

Quote:

Originally Posted by Snark1994 (Post 4808484)
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!!

ntubski 10-18-2012 02:26 AM

Quote:

Originally Posted by meninvenus (Post 4808735)

echo | awk "BEGIN{print 100 - $var2*100/$var1}"

The echo | should not be necessary when using BEGIN.

David the H. 10-20-2012 10:17 AM

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 `..`.


All times are GMT -5. The time now is 12:40 PM.