Currency Exchange

February 27, 2015

Instead of writing a program, the easiest way to complete the task is to use wget to fetch the data and awk to format it:

$ cat >currency
wget -qO- "$1&to=$2" |
awk -vAMT="$3" -F'[ ,]' '{printf "%.2f", $5 * AMT}'
$ chmod +x currency
$ ./currency USD GBP 1000

The currency command takes three parameters: the from currency, the to currency, and the from amount; currency is specified by standard three-letter codes. The flags to wget specify quiet mode with output (that’s a capital-letter oh, not a zero) to standard output. Output from the wget command looks like this:

{"to": "GBP", "rate": 0.64380199999999999, "from": "USD"}

The awk command splits the output into fields; the fifth field is the conversion rate, and simple multiplication does the rest.

The trick here is the url that provides the data, which I found by googling for “currency exchange api”. With baseball season starting in a few weeks, I will soon be googling for “baseball standings api”.


Pages: 1 2

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

%d bloggers like this: