With thanks to those who tried to help, the final script for my Bash problem is below. Problem:
To grab the fund price from the L&G website for an index tracker. To email the price to me, and to store that price in a CSV file to allow easy import to Quicken. There is no ticker available for auto download to quicken that I could find, so the ticker is made up. (LGTRKFTSE). This means the prices are associated with the correct share.
The script may not be the most elegant of solutions, but it works. There is a slight modification from the version I arrived at in the previous post - that is I needed to shift the decimal point, as the price is quoted in pence, but I need it in pounds.
Note that ^M is entered as ctrl-V ctrl-M
# Get the fund prices for L&G tracker and email me daily
# Get the file cd ~ wget -q http://www.legalandgeneral.com/investment/fundprice1_index.jsp --output-document=fundprices.txt
# Find the data I want grep --after-context=10 "UK Index Trust (Acc) (R)" fundprices.txt | sed -e :a -e 's/<[^>]*>//g;/ ~/fundprices.txt
# Trim Excess tabs tr -d '\t' < fundprices.txt > fundpricesout.txt
# I only want the closing price grep -m 1 '[0-9]' ~/fundpricesout.txt > fundprices.txt
# Shift the decimal point cp fundprices.txt fundpricesout.txt sed 's/[0-9][0-9]\./\.&/g' fundpricesout.txt | sed 's/\.//2' > fundprices.txt
# Output the CSV record for easier import to Quicken echo -n "LGTRKFTSE," >>fundprice.csv cat fundprices.txt >> fundprice.csv echo -n $(date +%d/%m/%Y) >> fundprice.csv echo "BRK" >> fundprice.csv
# Strip off the ctrl M characters sed 's/^M//g' fundprice.csv > fundpricesout.txt cp fundpricesout.txt fundprice.csv
# This bit reformats the file cat fundprice.csv | tr '\n' ',' > fundpricesout.txt sed 's/BRK,/\n/g' fundpricesout.txt > fundprice.csv cp fundprice.csv fundpricesout.txt sed 's/,LG/\nLG/g' fundprice.csv > fundpricesout.txt # The above is only needed as for some silly reason I couldn't get # rid of the newline in the file containing the price # It's not pretty, but it works
grep '[0-9]\.[0-9]' fundpricesout.txt > fundprice.csv # This bit cleans out any lines without a price. # It sometime happens if there is a network problem, and I am happy to miss a datapoint # as long as the file is in the right format.
# Mail me the price for the day mail -s "Prices for `date +%Y-%m-%d`" email@example.com < ~/fundprices.txt
# Tidy up a bit rm fundpricesout.txt rm fundprices.txt
This is set to run as a cron job on weekdays. Monthly, I am emailed the csv file.