Get latest exchange rate using command line?

4

1

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?

Anderson Nascimento Nunes

Posted 2013-12-28T16:47:49.523

Reputation: 506

Answers

1

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
    

Synetech

Posted 2013-12-28T16:47:49.523

Reputation: 63 242

hi, when I try to use this command it returns ERROR 403 forbidden. does it is possible that now (2018) google is preventing calls from non-browser clients? is that service or other equivalent available? – cesarpachon – 2018-04-27T11:32:14.060

1

That command should work in (almost) any UNIX or in Git Bash in Windows. If you don't have wget, then you can use curl instead:

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

You can save to a file with:

the_cmd > rate.txt

Or variable with:

rate=$(the_cmd)

You might want to suppress stderr of the commands to reduce the noise. You can do that by redirecting their stderr to /dev/null, like this:

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

Not sure if you need anything else. I don't know of a better tool to do this, in neither Windows nor *nix.

janos

Posted 2013-12-28T16:47:49.523

Reputation: 2 449