problem with multiplication in awk

problem with multiplication in awk

Post by Aviapi » Tue, 07 May 2002 20:44:09



I have (to practice awk programming) to create a simple calculator
with awk
here is my code:

command : echo "34 * 2" | bc-l.sh

script :
read a
echo $a | awk '
{

    { if($2=="+") { sum = $1+=$3; print  sum;} }
    { if($2=="-") { sum = $1-=$3; print  sum;} }
    { if($2=="/") { sum = $1/=$3; print  sum;} }
    { if($2=="*") { sum = $1*=$3; print  sum;} }

Quote:}'

It works well except with multiplication. The * character seems to be
interpreted like "joker" not "multiplication" How could I fix it?

Thank You

 
 
 

problem with multiplication in awk

Post by Conrad Sabatie » Tue, 07 May 2002 21:54:33



[snip]

Quote:>It works well except with multiplication. The * character seems to be
>interpreted like "joker" not "multiplication" How could I fix it?

This is due to shell globbing.

You need to tell the shell to treat the '*' character as an ordinary
character instead of as a wildcard.  How to do this depends on the shell
you're using.

Check the man pages for your particular shell.
--


 
 
 

problem with multiplication in awk

Post by Ben Altma » Tue, 07 May 2002 22:30:59



Quote:> I have (to practice awk programming) to create a simple calculator
> with awk
> here is my code:

> command : echo "34 * 2" | bc-l.sh

> script :
> read a
> echo $a | awk '
> {

>     { if($2=="+") { sum = $1+=$3; print  sum;} }
>     { if($2=="-") { sum = $1-=$3; print  sum;} }
>     { if($2=="/") { sum = $1/=$3; print  sum;} }
>     { if($2=="*") { sum = $1*=$3; print  sum;} }
> }'

> It works well except with multiplication. The * character seems to be
> interpreted like "joker" not "multiplication" How could I fix it?

Quote the variable so * isn't expanded.
Ben
 
 
 

problem with multiplication in awk

Post by bta.. » Wed, 08 May 2002 04:51:04





> > I have (to practice awk programming) to create a simple calculator
> > with awk
> > here is my code:

> > command : echo "34 * 2" | bc-l.sh

> > script :
> > read a
> > echo $a | awk '
> > {

> >     { if($2=="+") { sum = $1+=$3; print  sum;} }
> >     { if($2=="-") { sum = $1-=$3; print  sum;} }
> >     { if($2=="/") { sum = $1/=$3; print  sum;} }
> >     { if($2=="*") { sum = $1*=$3; print  sum;} }
> > }'

> > It works well except with multiplication. The * character seems to be
> > interpreted like "joker" not "multiplication" How could I fix it?

> Quote the variable so * isn't expanded.
> Ben

I quoted the $a instead and it works.

------- Sample Output -------

Script started on Mon May  6 12:48:53 2002
[ /students/btam01/tmp ]
cat calc

read a
echo "$a" | awk '
{
    { if ( $2 == "+" ) { sum = $1 + $3; print sum;} }
    { if ( $2 == "-" ) { sum = $1 - $3; print sum;} }
    { if ( $2 == "/" ) { sum = $1 / $3; print sum;} }
    { if ( $2 == "*" ) { sum = $1 * $3; print sum;} }

Quote:}'

[ /students/btam01/tmp ]
echo "3 * 3" | calc
9
[ /students/btam01/tmp ]

script done on Mon May  6 12:49:09 2002

-Bill

 
 
 

problem with multiplication in awk

Post by those who know me have no need of my nam » Thu, 16 May 2002 11:38:39



Quote:>I have (to practice awk programming) to create a simple calculator
>with awk

i would be tempted to use:


--
bringing you boring signatures for 17 years

 
 
 

problem with multiplication in awk

Post by those who know me have no need of my nam » Thu, 16 May 2002 13:31:36




>>I have (to practice awk programming) to create a simple calculator
>>with awk

>i would be tempted to use:




--
bringing you boring signatures for 17 years

 
 
 

1. Awk, multiplication, help please!

Your help would be appreciated!

I need to multiply $5 in the following AWK command (ie append a "0"
if there is no decimal or move the decimal one space to the right
if there is one. )

cat in.file | awk '{ print $1 " " $2 " " $3 " " $4 " " $5 }'> out.file

As you can see from the above, I am really new at this and
do need some help.

Thanks in advance!

-Neal.

2. c sources for ping

3. Threads Matrix Multiplication

4. remove a corrupted dir?

5. large integer divide/multiplication

6. Prodaja Linux distribucija - veliki izbor i niske cene......

7. Help on shellscript multiplication

8. recursive searching

9. Multiplication using (k)sh? How?

10. matrix multiplication

11. how to compile multiplication and division operations not supported by CPU

12. Floating Point Multiplication

13. supraexpress modems (28.8) problems w/dial-ins