Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Convert any currency via the command line UNIX
Create the following shell script somewhere in your PATH, make it executable (chmod 755 script_name), and you've got a simple currency converter in the Terminal. Note that you'll need to have lynx installed to use it -- it's available via Fink. Note also that the /sw/bin... line below should be one long line; remove the line break, and don't add any spaces when doing so.
#!/bin/tcsh -f 

/sw/bin/lynx -nolist -dump 'http://finance.yahoo.com/currency/convert?amt='$1'
&from='$2'&to='$3'&submit=Convert' | grep $2$3 | sed -e 's/[ ]*//' | awk '{print $6}'  

exit 0
You'll need to know the abbreviations for each currency to use the script -- here's a list; these are also visible in the pop-up on the Yahoo Currency Converter. To convert, use the program like this:
conv_curr amount from_unit to_unit
For example, here's how to convert 100 US Dollars (USD) into Gambian Dalasis (GMD):
$ conv_curr 100 USD GMD
2,285.00
[robg adds: This hint, as submitted, consisted of only the script and the list of country codes, so nearly all of what you see above (including any mistakes!) is my writing. This should work just as well with links instead of lynx, and it should be possible to do something similar with curl, though I suspect it would take more work.]
    •    
  • Currently 2.20 / 5
  You rated: 1 / 5 (5 votes cast)
 
[11,678 views]  

Convert any currency via the command line | 6 comments | Create New Account
Click here to return to the 'Convert any currency via the command line' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Convert any currency via the command line
Authored by: simonpie on Jul 07, '05 02:34:33PM
Here is my modification where curr_units.txt is simply that file. So I just copied that script in ~/.bin and the currency file in ~/.bin to. That way, using the flag -H I can get the currency list.

#!/bin/tcsh -f 

if($1 == -H) then
cat ~/.bin/curr_units.txt
else lynx -nolist -dump 'http://finance.yahoo.com/currency/convert?amt='$1'&from='$2'&to='$3'&submit=Convert' | grep $2$3 | sed -e 's/[ ]*//' | awk '{print $6}'  
endif
exit 0


[ Reply to This | # ]
Convert any currency via the command line
Authored by: powerbookg3user0 on Jul 07, '05 09:11:34PM
Hmmm… I can make it better ;)
#!/bin/tcsh -f

if($1 == -h) then
lynx -nolist -dump 'http://www.macosxhints.com/dlfiles/curr_units.txt'
else lynx -nolist -dump 'http://finance.yahoo.com/currency/convert?amt='$1'
&from='$2'&to='$3'&submit=Convert' | grep $2$3 | sed -e 's/[ ]*//' | awk '{print $6}'
endif
exit 0
Again, make sure the else line and the &from line have no break inbetween. This doesn't need a local file, but instead, lynxes it!

---
Takumi Murayama

[ Reply to This | # ]

A "one-liner" for those without Lynx/Links
Authored by: nicksay on Jul 08, '05 10:40:57AM
If you don't want to install one of the text-based browsers, here's a (long) "one-liner" you can stick in your .profile
currency() { curl -s "http://finance.yahoo.com/currency/convert?amt=$1&from=$2&to=$3" \
  | sed -n '28 p' | awk 'BEGIN {FS="</table>"} {print $12}' \
  | awk -v f=$2 -v t=$3 'BEGIN {FS="<td[^>]*>(<b>)?|(</b>)?</td>"} {print $17,f,"=",$23,t}'; }
It's split into multiple lines for display, but it should be one long line. The backslashes should allow for proper copy & pasting.

You can call it via: currency 1 USD EUR



[ Reply to This | # ]
A "one-liner" for those without Lynx/Links
Authored by: wallybear on Jul 11, '05 04:29:08AM

....funny, I tried something similar with awk, but the field separator (FS) variable considered only the first char of the string submitted, so I had to solve the problem in another way (some posts below).



[ Reply to This | # ]
A TRUE One-Liner for Those without Lynx, etc
Authored by: nicksay on Jul 10, '05 10:09:01PM
So I came up with an more condensed version of my currency function from above. This one takes advantage of a different Yahoo API that gives straight CSV text and skips having to parse all that html.
currency() { printf "$1 $2 = %.4f $3\n" $(echo "$1 * "$(curl -s \
"http://finance.yahoo.com/d/quotes.csv?s=${2}${3}=X&f=l1" | tr -Cd "0-9.") | bc); }
As before, it's on two lines for legibility, with the line-break preceded by a back-slash. Remove the line-break and the back-slash. Stick it in your .profile and use via:
currency AMOUNT FROM TO
example:
currency 1 USD EUR


[ Reply to This | # ]
Using curl instead of lynx...
Authored by: wallybear on Jul 11, '05 04:21:01AM
Here is a possible quick and dirty variant that doesn't need to install lynx. Yes, it could be better done, by the way it works:

#!/bin/tcsh -f 

echo -n "$2 $1 = $3 " 
curl -s 'http://finance.yahoo.com/currency/convert?amt='$1'&from='$2'&to='$3'&submit=Convert' | tr "^" "_" | sed 's/yfnc_tablehead1/^/g' | tr "^" "\n" | grep $2$3 | sed 's//^/g' | tr "^ " "\t_" | awk '{ print $6 }' | sed 's/]*>//g'  

exit 0
(there are no line breaks from curl to ..//g')

As sed does not allow to use \n in the substitution string I had to use a workaround with tr.


[ Reply to This | # ]