RSS

[awk] Command line calculator using awk

03 Nov

This is an update on our bash command line calculator posted a few days ago — except for the fact that this time we’ll use awk to do the calculation instead of bc. As I mentioned in that post, you may use python or ruby (irb) to do the same thing, but these tricks may be useful if you don’t ¬†have ruby or python installed (bc and awk, in general, come by default in any Unix or GNU/Linux distro).

First, create (or rewrite if you used our last trick) function “?”¬† as follows and put it in your ~/.bashrc file:

? () { awk "BEGIN{ print $* }" ;}

and make sure to reload your ~/.bashrc file (do the similar thing if you're using any other shell). [NOTE: ZSH does not like ``?'' as a function, so you might consider replacing it with something reasonable, e.g., ``compute'']

Now, if you want to calculate an expression, do it, for example, as

$ ? "2*3+4.0*(9.9+8.1)"

and don’t forget the quotes.

.

The advantage of this over bc is that you can use more arithmetic and trigonometric functions (link):

              atan2(y,x)     Arctan of y/x between -pi and pi.

	      cos(x)	     Cosine function, x in radians.

	      exp(x)	     Exponential function.

	      int(x)	     Returns x truncated towards zero.

	      log(x)	     Natural logarithm.

	      rand()	     Returns a random number between zero and one.

	      sin(x)	     Sine function, x in radians.

	      sqrt(x)	     Returns square root of x.

.
You may include variables as well in the function definition itself:

? () { awk "BEGIN{ pi = 4.0*atan2(1.0,1.0); degree = pi/180.0; print $* }" ;}

where we have defined the variable pi and degree (such that tan(pi/4.0) = 1.0 and pi radians is equivalent to 180 degrees) to be used later, e.g.

$ ? "cos(pi)"
$ ? "cos(90*degree)"

and I’m sure that you’ll get -1 and 0 (within the machine precision), respectively, as the answer!

(You may find some more interesting calculator related tricks posted in this blog scattered in different pages)

Credit: here and here (via LifeHacker).

UPDATE: I just realized that I can use the calc package (besides bc, awk, python and irb) to do the command line math wizardry more efficiently (and let’s take that as the end to this command line calculator series!). It has a larger set of built-in functions. You may grab the source code from the maintainer’s website and follow my instructions to install it on your system [although binaries are also readily available, e.g. apcalc package for Ubuntu 9.10 (Karmic Koala)].

About these ads
 
1 Comment

Posted by on November 3, 2009 in awk, cli, linux, osx, unix

 

Tags: , , , , , , , , , , , ,

One response to “[awk] Command line calculator using awk

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 34 other followers

%d bloggers like this: