<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ben&#039;s Blog</title>
	<atom:link href="http://benarmston.co.uk/blog/feed" rel="self" type="application/rss+xml" />
	<link>http://benarmston.co.uk/blog</link>
	<description>Programming, computers, me and my family</description>
	<lastBuildDate>Fri, 16 Oct 2009 10:24:50 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Live train times</title>
		<link>http://benarmston.co.uk/blog/2009/04/live-train-times</link>
		<comments>http://benarmston.co.uk/blog/2009/04/live-train-times#comments</comments>
		<pubDate>Mon, 06 Apr 2009 12:57:34 +0000</pubDate>
		<dc:creator>Ben Armston</dc:creator>
				<category><![CDATA[Command line]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://benarmston.co.uk/blog/?p=32</guid>
		<description><![CDATA[My day to day use of the command line doesn&#8217;t change much from one day to the next, and I sometimes forget just how fine a thing a powerful command line is. Every now and then, though I&#8217;m reminded just how much I like having the ability to connect together simple commands in ways that [...]]]></description>
			<content:encoded><![CDATA[<p>My day to day use of the command line doesn&#8217;t change much from one day to the next, and I sometimes forget just how fine a thing a powerful command line is. Every now and then, though I&#8217;m reminded just how much I like having the ability to connect together simple commands in ways that the original authors had never thought of. Recently, a simple pipeline I put together to get live train departure times was just such a reminder.</p>
<p><span id="more-32"></span></p>
<p>Up until February this year I was fortunate to receive a lift to work from one of my work colleagues.  Unfortunately he has now left the company <a href="#note_1">[1]</a>, leaving me to travel the 26 mile round trip by train and bike.  This isn&#8217;t such a bad thing though; I have been promising myself and my partner that I would take more exercise for such a long time that it had gotten to the point that it was only ever likely to happen if forced upon me.</p>
<p>There is only one train an hour, with the 17:12 train leaving 15 minutes before I could comfortably get to the station. However, it&#8217;s not unknown for the trains to be over 15 minutes late, so if I know that this train is delayed, it would be worth getting out the door on time and biking until my lungs explode to catch it.  So I have a need for up-to-date train times (and better lungs). </p>
<p>Live train times can be found on the <a href="http://www.livedepartureboards.co.uk">live departure boards</a> website; unfortunately, I&#8217;m just too lazy to check this often enough for it to be of any use.  What I really need is a program to check the website for me and inform me if the trains are running late. A few minutes work and I had created a simple pipeline to do just that.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">--quiet</span> <span style="color: #660033;">--output-document</span> - <span style="color: #ff0000;">'http://www.livedepartureboards.co.uk/ldb/sumdep.aspx?T=LMS&amp;S=COV&amp;A=1'</span> \
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">'headers=&quot;header[34]'</span> \
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span><span style="color: #ff0000;">'&gt;'</span> <span style="color: #660033;">-f2</span> \
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span><span style="color: #ff0000;">'&lt;'</span> <span style="color: #660033;">-f1</span> \
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'N; s/\n/ /'</span></pre></div></div>

<p>The <a href="http://linux.die.net/man/1/wget"><code>wget</code></a> command downloads the accessible version of the page containing the train times I&#8217;m interested in. The accessible version adds some extra attributes to the HTML tags making it much easier for me to find just the lines I want. They can be found by searching for any lines containing either <code>headers="header3</code> or <code>headers="header4</code>, which provides something similar to:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;td headers=&quot;header3 header7&quot;&gt;17:12&lt;/td&gt;
&lt;td headers=&quot;header4 header7&quot;&gt;On time&lt;/td&gt;
&lt;td headers=&quot;header3 header9&quot;&gt;18:12&lt;/td&gt;
&lt;td headers=&quot;header4 header9&quot;&gt;18:30&lt;/td&gt;</pre></div></div>

<p>The two <a href="http://linux.die.net/man/1/cut"><code>cut</code></a> commands first strip out everything before the first > and then everything after the first <, providing:</p>

<div class="wp_syntax"><div class="code"><pre class="madeup" style="font-family:monospace;">17:12
On time
18:12
18:30</pre></div></div>

<p>Finally, because I&#8217;m picky I call sed to join the lines together to give me the desired output:</p>

<div class="wp_syntax"><div class="code"><pre class="madeup" style="font-family:monospace;">17:12 On time
18:12 18:30</pre></div></div>

<p>The <a href="http://linux.die.net/man/1/sed"><code>sed</code></a> command consists of two instructions to sed. The first instruction, <code>N</code>, reads the next line from the input and appends it to the line of text that sed is currently working on. The second instruction, <code>s/\n/ /</code> substitutes the new line character used to join the two lines together for a single space. So when sed is working on the line &#8220;17:12&#8243; and the next line is &#8220;On time&#8221;, it first joins them together to form &#8220;17:12\nOn time&#8221; and then transforms it into &#8220;17:12 On time&#8221;.</p>
<p>You can see from the final output that the 17:12 train is on time, whilst the 18:12 is delayed until 18:30, so it looks like I&#8217;ll be staying in the warm office with the free coffee a little while longer.</p>
<p>There are a couple of easy ways to improve this script, I could either use <a href="http://linux.die.net/man/1/watch"><code>watch</code></a> to keep the output continuously updated.  Or use <a href="http://linux.die.net/man/5/crontab"><code>cron</code></a> and <a href="http://linux.die.net/man/1/zenity"><code>zenity</code></a> to inform me if either of those trains are not &#8220;On time&#8221;. I&#8217;m using watch at the moment and running it in a terminal which is visible on all of my virtual desktops. This is working well for me, apart from the small snag that I&#8217;m still waiting for the 17:12 to be delayed <img src='http://benarmston.co.uk/blog/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
<p><a name="note_1"></a>[1] He is currently looking for work, so if you are in the Edinburgh area and require a talented Linux sysadmin, head over to <a href="http://emeraldreverie.org/">his website</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://benarmston.co.uk/blog/2009/04/live-train-times/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>My first post</title>
		<link>http://benarmston.co.uk/blog/2009/03/first-post</link>
		<comments>http://benarmston.co.uk/blog/2009/03/first-post#comments</comments>
		<pubDate>Sat, 07 Mar 2009 20:53:42 +0000</pubDate>
		<dc:creator>Ben Armston</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://benarmston.co.uk/blog/?p=16</guid>
		<description><![CDATA[This post marks the official start of my blog and of my website.
My name is Ben Armston, I&#8217;m 31 years old and I live in the UK with my fiance and our two year old son. I work as a computer programmer writing a management appliance in Ruby. The primary interface to the appliance is [...]]]></description>
			<content:encoded><![CDATA[<p>This post marks the official start of my blog and of my website.</p>
<p>My name is Ben Armston, I&#8217;m 31 years old and I live in the UK with my fiance and our two year old son. I work as a computer programmer writing a management appliance in <a href="http://www.ruby-lang.org/">Ruby</a>. The primary interface to the appliance is web based, so I spend a lot of my time writing <a href="http://www.rubyonrails.org/">Ruby on Rails</a> code.</p>
<p>This website will be dedicated to improving my online presence and providing me with a means to demonstrate my technical skills in designing and implementing a website. My posts will cover programming related topics, web page design, random rants and anything else which takes my fancy.</p>
<p>The random rants are various thoughts that have entered my head and are refusing to leave. I will post about them here in an effort to offload them into the head of some poor unsuspecting visitor. </p>
<p>One thought that worries me a lot is the state of the economy and my ability to pay my mortgage and provide for my family should my job be made redundant. I&#8217;m probably most marketable as a web developer, so expect this site to change a lot in the coming weeks as I use it demonstrate my &#0252;ber webdev skills <img src='http://benarmston.co.uk/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p>If you want to know more about me you can read <a href="/blog/profile">my profile</a>.</p>
<p>Ben.</p>
]]></content:encoded>
			<wfw:commentRss>http://benarmston.co.uk/blog/2009/03/first-post/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
