Unix

Linux/Unix Command Line Help

I have the need to take a text file, and issue a command (for use in a script) which will automatically remove all the lines before (or after) a certain point in the text file. Example.

If the file constained FooFooFoo Fido BarBarBar, I could say, don't output anything until you see 'Fido' and then just output the file (which could be redirected to an output file). The result would be, BarBarBar (or possibly Fido BarBarBar)

Or I could say, output everything, until you see 'Fido', then stop (yielding FooFooFoo or possibly FooFooFoo Fido)

I'm sure this must be possible, but I can't see how to do it. Any ideas gratefully received.

Bash Problem - sorted

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`" myemail@foo.bar < ~/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.

Bash Problem, strange new line!

I've been working on a BASH problem. I have this little script which, on a weekday (set by crontab) will run. It will grab the price of a unit trust from the legal and general website, clean up the output and email me the price. I want to create a CSV file of all the prices (the TICKER is one of my own choosing - I've not been able to find the correct ticker which will automatically download the prices to quicken)

(Since I first posted this, I've tweaked the script a little, trying to solve the bug - to no avail - I've replaced the code with the version as of 4th Jan 2008 - the bug is still present - note, ^M is actually ctrl-V ctrl-M, not carat-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

# Output the CSV record for easier import to Quicken echo -n "LGTRKFTSE," >>fundprice.csv cat fundprices.txt >> fundprice.csv #BUG - THERE IS A NEW LINE CREEPING IN SOMEWHERE - IT NEEDS REMOVING echo -n "," >> fundprice.csv echo $(date +%d/%m/%Y) >> fundprice.csv

# Strip off the ctrl M characters sed 's/^M//g' fundprice.csv > fundpricesout.txt cp fundpricesout.txt fundprice.csv

# Mail me the price for the day mail -s "Prices for `date +%Y-%m-%d`" myemail@address.foo.bar < ~/fundprices.txt

# Tidy up a bit rm fundpricesout.txt rm fundprices.txt 

The script emails me correctly, but unfortunately, I can't get the CSV to work. The output file looks like this:

LGTRKFTSE,169.6 ,02/01/2008

Where it should look like this:

LGTRKFTSE,169.6,02/01/2008

How can I get rid of that annoying new line character? (I want to keep newlines between the entries for different days)

I'm sure it's something subtle but easy - but it's escaping me.... Anyone know the trick?