Of course, you don't need all that grbage: what you are looking
for are the line that contains Last Trade:
and 27.21 next to it. The trouble is, the HTML page
is not human-readable. Open it in vi and you'll see why:
it has very long lines without breaks. In fact, we are looking for
line 5.
The challange now is to exatract the price 27.21 . Note that
simply searching for <td class= or some other
non-specific text won't help as there are probably lots of those
symbols in this HTML page.
Here is an awk line that does the trick:
awk 'BEGIN{FS = ">Last Trade:</td><td class=\"yfnc_tabledata1\"><big><b>" }{if (NR == 5) print substr($2,1,5) }' page
Note that:
- We redefined FS so that 27.21 is
in the beginning of a new field.
- We use substr() function: you already saw it
in use in HW3. In this particular
case, it takes a substring of length 5, beginning at position 1 from
field 2. You can check that this points to 27.21 if applied
to the field that now begins with 27.21
- As is alsways the case, there are many other ways to
achieve the same result. The above is not necessarily the
most elegant or robust way.
Here is the complete bash script:
#!/bin/bash
# the script below "downloads" the webpage and parses it
# for the information we need.
# the search patterns below work only for stocks (like MSFT or GOOG),
# not mutual funds and other traded items
# The line below defines the URL and the query string
prefix="http://finance.yahoo.com/q?s="
for i
do
url=$prefix$i
wget $url -o /dev/null -O page
val=`awk 'BEGIN{FS = ">Last Trade:</td><td class=\"yfnc_tabledata1\"><big><b>" }{if (NR == 5) print substr($2,1,5) }' page`
echo "The stock of" "(" $i ") is currently priced at $"$val
done
Suppose you want the script to instruct you to buy the stock if its price
is below $100. Well, one problem is that bash can only operate with
integer numbers. But here is a trick: save your output in a file, and make a
call to gawk from within your script. To test this idea, add the following to the script above (inside the do loop):
rm data.out
echo $val >> data.out
/usr/bin/gawk '{ { if ($1 > 100 ) print "SELL!" } {if ($1 < 100 ) print "BUY!" } }' < data.out