Get latest exchange rate using command line

command linewget

I search for a way to get the last exchange rate online using the command line. I want to use it with another program. Until now all I have is this:

wget -qO- "http://www.google.com/finance/converter?a=1&from=usd&to=inr&hl=es" |  sed '/res/!d;s/<[^>]*>//g';

Not my code, found on web.

It will output to console, but I need to save it in a file or to a variable in other program that will call that command. I don't have experience with wget and couldn't find any other way to do what I want. Is there any program (Windows is preferred, but *nix is acceptable) to do that or is there a way with wget?

Best Answer

If you can find a site that provides forex information in an automation-friendly way, then you can forgo the call to sed altogether and just use the -O switch for wget. Until then, the command you found works fine with Windows ports of wget and sed; you only have to make a couple of little tweaks. First, you have to replace the single-quotes in the call to sed with double-quotes and remove the trailing semi-colon. Second, you need to redirect the final output to a file or environment variable instead of the console.

  • For console output:

    wget -qO- "google.com/finance/converter?a=1&from=usd&to=inr" | sed "/res/!d;s/<[^>]*>//g"
    
  • For file output (adjust filename and path as necessary):

    wget -qO- "google.com/finance/converter?a=1&from=usd&to=inr" | sed "/res/!d;s/<[^>]*>//g" > forex_%date%.log
    
  • For variable output (adjust variable name as necessary):

    for /f "delims=" %%i in ('wget -qO- "google.com/finance/converter?a=1&from=usd&to=inr" ^| sed "/res/!d;s/<[^>]*>//g"') do @set forex=%%i