4

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?

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
    
|improve this answer|||||
  • 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 Apr 27 '18 at 11:32
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.

|improve this answer|||||
-1

Since http://www.google.com/finance/converter is not working now, replace it with xe.com
The following command will print EUR/USD exchange rate.

curl -s "https://www.xe.com/currencyconverter/convert/\?Amount\=1\&From\=EUR\&To\=USD"|awk -F 'EUR / USD' '{printf substr($2,14,7);}'
|improve this answer|||||
  • Welcome on the SU! I suggest to give some explanation, this site tries to be more than a code snippet copy-paste forum. – peterh - Reinstate Monica Mar 26 at 10:35
  • We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed. – Tetsujin Mar 26 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.