<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Haskell, Linux and all that</title>
	<atom:link href="http://haskelladdict.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://haskelladdict.wordpress.com</link>
	<description>You&#039;ll likely find random Linux related things here, including some (hopefully) helpful tips and tricks. Once in a while I might even do a post about my favorite programming language, Haskell.</description>
	<lastBuildDate>Sat, 31 Dec 2011 12:00:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='haskelladdict.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Haskell, Linux and all that</title>
		<link>http://haskelladdict.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://haskelladdict.wordpress.com/osd.xml" title="Haskell, Linux and all that" />
	<atom:link rel='hub' href='http://haskelladdict.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Why pointers and arrays in C are not the same</title>
		<link>http://haskelladdict.wordpress.com/2009/10/11/why-pointers-and-arrays-in-c-are-not-the-same/</link>
		<comments>http://haskelladdict.wordpress.com/2009/10/11/why-pointers-and-arrays-in-c-are-not-the-same/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 19:01:28 +0000</pubDate>
		<dc:creator>haskelladdict</dc:creator>
				<category><![CDATA[C programming]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[pointer]]></category>

		<guid isPermaLink="false">http://haskelladdict.wordpress.com/?p=194</guid>
		<description><![CDATA[In a post in February I posted the following piece of code file1.c: int foobar[100]; file2.c: extern int *foobar; stating that it won&#8217;t work as expected. Back then I referred people to Peter van then Linden&#8217;s book &#8220;Expert C Programming&#8221; for a detailed exposition. However, since a few people have since asked for an explanation [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=194&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://haskelladdict.wordpress.com/2009/02/22/book-tip-expert-c-programming-by-peter-van-der-linden/">post</a> in February I posted the following piece of code<br />
<strong>file1.c</strong>:</p>
<pre class="literal-block">int foobar[100];</pre>
<p><strong>file2.c</strong>:</p>
<pre class="literal-block">extern int *foobar;</pre>
<p>stating that it won&#8217;t work as expected. Back then I referred people to Peter van then Linden&#8217;s book &#8220;Expert C Programming&#8221; for a detailed exposition. However, since a few people have since asked for an explanation I&#8217;ll attempt to give one below. Please refer to van then Linden&#8217;s book for a much more detailed and more well written exposition of the whole subject.</p>
<p>First off, it is very important to realize that arrays and pointers in C are not the same. Unfortunately there are many places in the C universe where this equivalence holds which has led many people to assume it is a general truth &#8211; it is not! So, once more, arrays and pointers in C are not the same! In other words, the two declarations</p>
<pre class="literal-block">extern int *x;
extern int y[];
</pre>
<p>mean something different to the compiler.</p>
<p>That said, let&#8217;s first think about how elements of</p>
<pre class="literal-block">int foo[] = "01234"
</pre>
<p>are accessed. In order to get at foo[2], say, we first retrieve the address of foo from the symbol table. Importantly, stored at this address is an int, namely, the first element of foo, foo[0]. Therefore, to get at foo[3] we simply start at foo and then grab the third int starting from that address. Hence, retrieving foo[3] involves two steps, get the address of foo itself, then march forward in memory to grab the 3rd int.</p>
<p>Now that that&#8217;s out of the way let&#8217;s consider</p>
<pre class="literal-block">int *bar = "01234"
</pre>
<p>and again think about what is happening under the hood when retrieving bar[3]. Here, we have told the compiler that bar is a pointer to an int. So when the compiler retrieves the address of bar from the symbol table, bar does not store an int like in the case of foo above. Rather, bar holds a pointer to int which in turn tells us where in memory this int actually is, quite literally an &#8220;int*&#8221;, really. This is the important distinction! Hence, accessing bar[3] involves one additional redirection step compared to a direct array access like in the case of foo[3]. First, the compiler retrieves the pointer to int stored at bar. Then we follow this pointer to get at the actual int, bar[0]. Finally, we march ahead three ints worth of memory to get at bar[3].</p>
<p>With this in mind let&#8217;s again look at the example given at the beginning:<br />
<strong>file1.c</strong>:</p>
<pre class="literal-block">int foobar[100];</pre>
<p><strong>file2.c</strong>:</p>
<pre class="literal-block">extern int *foobar;</pre>
<p>In file1.c we declare the symbol foobar to be an array of ints. Since foobar is an array, its address stored in the symbol table will directly point to the first int in the array, foobar[0] in this case.</p>
<p>Unfortunately, in file2.c we tell the compiler that foobar is a pointer to an int. So what will happen if we try to access foobar[2] somewhere inside file2.c? According to the above the following:</p>
<ol>
<li> Get the address of foobar from the symbol table</li>
<li> Retrieve the pointer to int stored at foobar</li>
</ol>
<p>Ups! That&#8217;s bad, foobar does not store a pointer to int, it stores ints directly. Hence, the additional redirection step caused by declaring foobar as &#8220;int*&#8221; causes the compiler to falsely interpret the first bunch of ints stored at foobar as a pointer to int. Obviously, when following this bogus pointer nothing really good is going to happen and we may end up with a potentially very hard to track down bug.</p>
<p>That said, the correct way to declare foobar in file2.c is of course<br />
<strong>file2.c</strong></p>
<pre class="literal-block">extern int foobar[];
</pre>
<p>Here, we&#8217;re telling the compiler that when retrieving the address of foobar from the symbol table it must directly interpret it as a memory location where ints a stored and all is well.</p>
<p>Hence, despite the fact that occasions abound where C will treat arrays as pointers (e.g., array names in expressions are treated as pointers), fundamentally they are different and it is important to keep this in mind.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/haskelladdict.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/haskelladdict.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/haskelladdict.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/haskelladdict.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/haskelladdict.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/haskelladdict.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/haskelladdict.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/haskelladdict.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/haskelladdict.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/haskelladdict.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/haskelladdict.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/haskelladdict.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/haskelladdict.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/haskelladdict.wordpress.com/194/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=194&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://haskelladdict.wordpress.com/2009/10/11/why-pointers-and-arrays-in-c-are-not-the-same/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">haskelladdict</media:title>
		</media:content>
	</item>
		<item>
		<title>Having fun sorting with Haskell</title>
		<link>http://haskelladdict.wordpress.com/2009/07/11/having-fun-sorting-with-haskell/</link>
		<comments>http://haskelladdict.wordpress.com/2009/07/11/having-fun-sorting-with-haskell/#comments</comments>
		<pubDate>Sat, 11 Jul 2009 16:25:17 +0000</pubDate>
		<dc:creator>haskelladdict</dc:creator>
				<category><![CDATA[C programming]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://haskelladdict.wordpress.com/?p=171</guid>
		<description><![CDATA[Recently, I re-discovered my good old copy of Jon Bentley&#8217;s Programming Pearls and started re-reading some of the chapters. It is a wonderful little book which I can highly recommend to any programmer. Particularly, the chapter on sorting (Column 11) is great and I decided to implement some common sorting routines in C just to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=171&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently, I re-discovered my good old copy of Jon Bentley&#8217;s <em>Programming Pearls </em>and started re-reading some of the chapters. It is a wonderful little book which I can highly recommend to any programmer. Particularly, the chapter on sorting (Column 11) is great and I decided to implement some common sorting routines in C just to stay in practice. Pretty quickly I had written a <em>bubble sort</em>, <em>insertion sort</em>, and <em>quicksort</em> routine and tested them all with a random vector of ints of length 20,000. Of course, <em>quicksort</em> (combined with <em>insertion sort</em> to sort the smallest sub-array) beat the others hands down.</p>
<p>So far so good but why not implement these sorting routines in Haskell as well, just for fun. <em>Quicksort</em> in Haskell is straightforward and strikingly beautiful. Here it is</p>
<p>&gt; quicksort :: [Double] -&gt; [Double]<br />
&gt; quicksort [] = []<br />
&gt; quicksort (x:xs) = (quicksort (filter (&lt;= x) xs)) ++ [x]<br />
&gt;                                    ++ (quicksort (filter (&gt; x) xs))</p>
<p>Interestingly, even though this implementation uses filter twice, it is a tad faster on my system (~ 3%) than the version that uses a single call to partition to partition the list.</p>
<p>Of course, this version is no match for the C implementation in terms of speed due to the appends and the fact that each recursive call to <em>quicksort</em> uses its own private copy of the sub-array. Nevertheless, it is quite speedy and code-wise it can&#8217;t get much more concise.</p>
<p>My Haskell implementation of <em>insertion sort</em> is also not too much longer</p>
<p>&gt; insertion_sort :: [Double] -&gt; [Double]<br />
&gt; insertion_sort []       = []<br />
&gt; insertion_sort (x:xs) = my_insert x (insertion_sort xs)<br />
&gt;<br />
&gt;     where<br />
&gt;         my_insert a []                       = [a]<br />
&gt;        my_insert a tot@(b:bs) = case compare a b of<br />
&gt;                                                                                                  GT -&gt; b:(my_insert a bs)<br />
&gt;                                                                                                      _  -&gt; a:tot</p>
<p>Yes, I know, I could have used prelude&#8217;s insert function instead, but<br />
for some reason, using my_insert is about 30% faster for a reason I don&#8217;t understand since the functions are almost identical. The only reason I can think of is the additional redirection via insertBy used by insert. Of course, due to its O(n<sup>2</sup>) behaviour, <em>insertion sort</em> is quite a bit slower than <em>quicksort</em>.</p>
<p>Finally, there is my version of <em>bubble sort</em> and, frankly, it took me some time to find an implementation that ran in a somewhat decent amount of time without blowing my machine&#8217;s stack. My code threads a status variable through the bubble function which signals the absence of swaps indicating a sorted list. Here it is</p>
<p>&gt; bubble_sort :: [Double] -&gt; [Double]<br />
&gt; bubble_sort xs   = let<br />
&gt;                                                               (state,bubbled) = bubble True xs<br />
&gt;                                                           in<br />
&gt;                                                               if not state<br />
&gt;                                   then bubble_sort bubbled<br />
&gt;                                   else bubbled<br />
&gt;<br />
&gt;   where<br />
&gt;     bubble s []                = (s,[])<br />
&gt;          bubble s [a]              = (s,[a])<br />
&gt;         bubble s (a:b:as) = case compare a b of<br />
&gt;                                                                            GT -&gt; let<br />
&gt;                                                                                                      (s_n,new) = bubble False (a:as)<br />
&gt;                                                                                                 in<br />
&gt;                                                                                                     (s_n,b:new)<br />
&gt;<br />
&gt;                                                                              _  -&gt; let<br />
&gt;                                                                                                    (s_n,new) = bubble (s &amp;&amp; True) (b:as)<br />
&gt;                                               in<br />
&gt;                                                                                                     (s_n,a:new)</p>
<p>This version of<em> bubble sort</em> is about 6 times slower than insertion sort. So not too bad but I have the feeling it can still be improved quite a bit. E.g., currently bubble walks through the whole list during each iteration even the already sorted part which is a waste.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/haskelladdict.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/haskelladdict.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/haskelladdict.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/haskelladdict.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/haskelladdict.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/haskelladdict.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/haskelladdict.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/haskelladdict.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/haskelladdict.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/haskelladdict.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/haskelladdict.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/haskelladdict.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/haskelladdict.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/haskelladdict.wordpress.com/171/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=171&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://haskelladdict.wordpress.com/2009/07/11/having-fun-sorting-with-haskell/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">haskelladdict</media:title>
		</media:content>
	</item>
		<item>
		<title>TABA pattern in Haskell</title>
		<link>http://haskelladdict.wordpress.com/2009/06/06/taba-in-haskell/</link>
		<comments>http://haskelladdict.wordpress.com/2009/06/06/taba-in-haskell/#comments</comments>
		<pubDate>Sat, 06 Jun 2009 15:17:59 +0000</pubDate>
		<dc:creator>haskelladdict</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Danvy]]></category>
		<category><![CDATA[Goldberg]]></category>
		<category><![CDATA[symbolic convolution]]></category>
		<category><![CDATA[TABA pattern]]></category>

		<guid isPermaLink="false">http://haskelladdict.wordpress.com/?p=158</guid>
		<description><![CDATA[After a recent discussion on Haskell-Cafe I read over the excellent paper entitled There and Back Again (TABA) by Danvy and Goldberg and was quite fascinated. So what is TABA? In a nutshell, the TABA pattern utilizes a recursive function to traverse a data structure such as a list at return time. This allows one [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=158&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After a recent discussion on Haskell-Cafe I read over the excellent paper entitled <a href="http://www.brics.dk/RS/02/12/BRICS-RS-02-12.pdf">There and Back Again</a> (TABA) by Danvy and Goldberg and was quite fascinated.</p>
<p>So what is TABA? In a nutshell, the TABA pattern utilizes a recursive function to traverse a data structure such as a list at return time. This allows one to, e.g.,  convolve two list [x<sub>1</sub>, x<sub>2</sub>, ..., x<sub>n</sub>] and [y<sub>1</sub>, y<sub>2</sub>, ..., y<sub>n</sub>] to yield</p>
<p>[(x<sub>1</sub>,y<sub>n</sub>), (x<sub>2</sub>,y<sub>n-1</sub>), ..., (x<sub>n</sub>,y<sub>1</sub>)]</p>
<p>in only <em>n</em> recursive calls.</p>
<p>So how does this work? The main idea is that during the recursive traversal of the first list we build up a stack of functions each of which is responsible for generating one of the final list elements. During stack unwinding these function are being replayed and generate the final result. The authors of the TABA paper liken it to winding a spring during the recursive traversal. If this sounds a bit too abstract here&#8217;s a Haskell implementation of a list convolution function using the TABA pattern</p>
<p>&gt;convolve :: [a] -&gt; [b] -&gt; [(a,b)]<br />
&gt;convolve xs ys = fst . walk $ xs<br />
&gt;  where<br />
&gt;       walk []              = ([],ys)<br />
&gt;       walk (a:as) = let<br />
&gt;                                               (r, (b:bs)) = walk as<br />
&gt;                                          in<br />
&gt;                                              ((a,b):r, bs)</p>
<p>Isn&#8217;t that great!? Even better, the above can nicely be written as a fold like so</p>
<p>&gt;convolve1 :: [a] -&gt; [b] -&gt; [(a,b)]<br />
&gt;convolve1 xs ys = fst . foldr (\x (r,(a:as)) -&gt; ((x,a):r,as)) ([],ys) $ xs</p>
<p>If this has whet your appetite please consult the TABA paper for a much better and more in depth exposition. Enjoy!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/haskelladdict.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/haskelladdict.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/haskelladdict.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/haskelladdict.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/haskelladdict.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/haskelladdict.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/haskelladdict.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/haskelladdict.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/haskelladdict.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/haskelladdict.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/haskelladdict.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/haskelladdict.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/haskelladdict.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/haskelladdict.wordpress.com/158/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=158&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://haskelladdict.wordpress.com/2009/06/06/taba-in-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">haskelladdict</media:title>
		</media:content>
	</item>
		<item>
		<title>How awk can make your life easier.</title>
		<link>http://haskelladdict.wordpress.com/2009/04/04/how-awk-can-make-your-life-easier/</link>
		<comments>http://haskelladdict.wordpress.com/2009/04/04/how-awk-can-make-your-life-easier/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 14:55:55 +0000</pubDate>
		<dc:creator>haskelladdict</dc:creator>
				<category><![CDATA[awk]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://haskelladdict.wordpress.com/?p=141</guid>
		<description><![CDATA[Sometimes I have trouble imagining how to get things done efficiently without all the wonderful little (or not so little) unix command line tools, perhaps chained together with a few pipes inside a bash loop. However, if I had to choose a single utility to take with me on a desert island I would not [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=141&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes I have trouble imagining how to get things done efficiently without all the wonderful little (or not so little) unix command line tools, perhaps chained together with a few pipes inside a bash loop. However, if I had to choose a single utility to take with me on a desert island I would not hesitate and pick awk. If you haven&#8217;t ever used awk, chances are that you really should. Hopefully, the following will help you decide if awk could help make your computing life a bit easier.</p>
<p>In the first installation I will only provide a high level introduction to awk; next time we&#8217;ll dive in a bit deeper.  Before we get started it is probably prudent to point out that there are several implementations of awk floating around these days. If you use Linux, chances are that you will have GNU awk, or gawk for short, available. I will use gawk in my examples.</p>
<p>After these preliminaries, let&#8217;s talk about what awk actually does. As it turns out, awk is not simply a tool but an actual programming language designed to efficiently deal with the content of (plain text) files in a line by line fashion. Whereas other languages (python, ruby, C, ..) require one to explicitly open files and read their content line by line, awk provides this infrastructure for free and furthermore splits each line into fields according to a given separator &#8212; space by default. Hence, whenever you find yourself reading files and then doing something to each line, awk may help make your life considerably easier.  Let&#8217;s look at an example data file</p>
<pre class="literal-block"># cat my_data.txt
01/01/2009 234.23 100
01/02/2009 221.11  50
01/03/2009 123.11  34
...</pre>
<p>Here, each line contains three columns separated by a space. The first one is a date, the second one the dollar amount for a certain sales item, and the third the number of items sold. How would we go about converting this file into one that lists the total sales for each date in the first column and the corresponding date in the second one?</p>
<p>Sure, you could write a little python script but lets use awk instead. I mentioned above that awk splits each line into fields. These can be accessed via $0, $1, $2, &#8230;, where $0 holds the complete line, $1 the first field, $2 the second one, and so on. Hence,</p>
<pre class="literal-block"># gawk '{ print $0 }' my_data.txt</pre>
<p>will print each line of my_data.txt to stdout. awk automagically walks through all lines of the input file and applies the commands inside the curly braces to each line. That said, it&#8217;s now dead simple to write down the code to transform our sales example. Here it is</p>
<pre class="literal-block"># gawk '{print $2*$3,$1}' my_data.txt</pre>
<p>For each line we simply print token $2 (per item price) multiplied by token $3 (number of items) followed by $1 which is the date. Our output therefore consists of two columns; the comma makes sure that they are separated by a space.</p>
<p>As you can see, if you need to process column centric data files awk really can make your life a lot easier. If this has wet your appetite for awk you might be interested in the next installation on this blog. If you&#8217;re too impatient google is your friend or have a look at the excellent book by Dougherty and Robbins [1].</p>
<p>[1] <a href="http://oreilly.com/catalog/9781565922259/">sed &amp; awk, 2nd Ed., Dale Dougherty, Arnold Robbins</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/haskelladdict.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/haskelladdict.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/haskelladdict.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/haskelladdict.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/haskelladdict.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/haskelladdict.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/haskelladdict.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/haskelladdict.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/haskelladdict.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/haskelladdict.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/haskelladdict.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/haskelladdict.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/haskelladdict.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/haskelladdict.wordpress.com/141/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=141&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://haskelladdict.wordpress.com/2009/04/04/how-awk-can-make-your-life-easier/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">haskelladdict</media:title>
		</media:content>
	</item>
		<item>
		<title>Book Tip: &#8220;Expert C Programming&#8221; by Peter van der Linden</title>
		<link>http://haskelladdict.wordpress.com/2009/02/22/book-tip-expert-c-programming-by-peter-van-der-linden/</link>
		<comments>http://haskelladdict.wordpress.com/2009/02/22/book-tip-expert-c-programming-by-peter-van-der-linden/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 18:22:49 +0000</pubDate>
		<dc:creator>haskelladdict</dc:creator>
				<category><![CDATA[Book Tip]]></category>
		<category><![CDATA[C programming]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[Expert C Programming]]></category>
		<category><![CDATA[Peter Van Der Linden]]></category>

		<guid isPermaLink="false">http://haskelladdict.wordpress.com/?p=122</guid>
		<description><![CDATA[During the year I tend to read quite a few programming books. Similar to any other book genre, there are good ones and not so good ones. Once in a while though, I come across a real gem of the kind that makes me wish I&#8217;d read it a long time ago. Expert C Programming [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=122&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>During the year I tend to read quite a few programming books. Similar to any other book genre, there are good ones and not so good ones. Once in a while though, I come across a real gem of the kind that makes me wish I&#8217;d read it a long time ago. <em>Expert C Programming</em> by Peter van der Linden is one of these rare gems [1].</p>
<p>Van der Linden is a former developer for Sun Microsystems and has combined his real world experience with the C programming language into <em>Expert C Programming</em>. The book is packed with helpful insights of the kind missing from most other C books I&#8217;ve read &#8211; any serious C programmer should definitely have it on her bookshelf. Despite being published in 1994 most of the book&#8217;s advice still applies to this very day [2]. Be aware that <em>Expert C Programming</em> is not a C tutorial and the reader should already have a good grasp of the language to get the most out of it. So, if you&#8217;ve ever wondered why</p>
<p><strong>file1.c</strong>:</p>
<pre class="literal-block">int foobar[100];</pre>
<p><strong>file2.c</strong>:</p>
<pre class="literal-block">extern int *foobar;

/* piece of code that does something with foobar */</pre>
<p>won&#8217;t work go get yourself a copy of <em>Expert C Programming</em>.</p>
<h1>References</h1>
<p>[1] <a href="http://www.afu.com">http://www.afu.com</a><br />
[2] Interestingly, C99 does address some of the C shortcomings discussed in the book.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/haskelladdict.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/haskelladdict.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/haskelladdict.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/haskelladdict.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/haskelladdict.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/haskelladdict.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/haskelladdict.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/haskelladdict.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/haskelladdict.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/haskelladdict.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/haskelladdict.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/haskelladdict.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/haskelladdict.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/haskelladdict.wordpress.com/122/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=122&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://haskelladdict.wordpress.com/2009/02/22/book-tip-expert-c-programming-by-peter-van-der-linden/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">haskelladdict</media:title>
		</media:content>
	</item>
		<item>
		<title>How to *not* name your octave scripts</title>
		<link>http://haskelladdict.wordpress.com/2009/02/08/what-not-to-do-when-using-octave/</link>
		<comments>http://haskelladdict.wordpress.com/2009/02/08/what-not-to-do-when-using-octave/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 17:54:21 +0000</pubDate>
		<dc:creator>haskelladdict</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[default path]]></category>
		<category><![CDATA[GNU octave]]></category>
		<category><![CDATA[octave]]></category>
		<category><![CDATA[overloading]]></category>

		<guid isPermaLink="false">http://haskelladdict.wordpress.com/?p=112</guid>
		<description><![CDATA[Last week, I helped a co-worker debug one of her analysis scripts that used GNU octave. The script worked fine until one day it just stopped. Pretty quickly it turned out that a call to the std function which calculated the row-wise standard deviation of a fairly big matrix crashed for some unknown reason. The [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=112&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Last week, I helped a co-worker debug one of her analysis scripts that used <a href="http://www.gnu.org/software/octave/">GNU octave</a>. The script worked fine until one day it just stopped. Pretty quickly it turned out that a call to the <em>std </em>function which calculated the row-wise standard deviation of a fairly big matrix crashed for some unknown reason. The error messages that octave spewed at us were quite unhelpful so we were stumped. Since the size of the matrix had recently been doubled we tried pruning it down which did no good. I fired up another octave session in a different terminal, entered a small matrix by hand, tried to use std and &#8230;. it worked like a charm. So maybe something was wrong with the big matrix? After another 30 minutes we convinced ourselves that this was not the case and, furthermore, the script worked just fine on a different machine. Fair enough. So I moved the data plus script into /tmp and tried to run it there and &#8211; voila &#8211; it did its job without any problems.</p>
<p>Finally, it dawned on me what had happened. When octave executes commands either in the interactive shell or by running a script, it will look for the corresponding .m files in a number of directories (customizable by the <em>addpath</em> command) including the current directory. Importantly, it will (at least on my systems) try &#8216;.&#8217; <strong>first</strong>. Hence, if your current directory contains a script <em>foo.m</em> it will overload octave&#8217;s foo command and instead execute whatever is in <em>foo.m</em>, which probably isn&#8217;t what you want at all. To make things more subtle, in our case the problem wasn&#8217;t even a <em>std.m</em> file that overloaded the real <em>std</em>, but rather a <em>mean.m</em> file that would overload the real <em>mean</em> which was called from somewhere inside <em>std</em>. After renaming my co worker&#8217;s version of <em>mean.m</em> (which, by the way did not really calculate the mean) everything was fine again and the original script worked as expected.</p>
<p>The moral of the story is that you should <strong>never</strong> choose names for your .m files that match actual octave commands unless you know what you are doing &#8211; or like surprises. Doing so will silently overload the real commands with your versions. Make it a habbit to always prefix your commands in an obvious manner, e.g. <em>my_mean.m </em>instead of <em>mean.m</em> or something similar.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/haskelladdict.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/haskelladdict.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/haskelladdict.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/haskelladdict.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/haskelladdict.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/haskelladdict.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/haskelladdict.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/haskelladdict.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/haskelladdict.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/haskelladdict.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/haskelladdict.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/haskelladdict.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/haskelladdict.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/haskelladdict.wordpress.com/112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=112&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://haskelladdict.wordpress.com/2009/02/08/what-not-to-do-when-using-octave/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">haskelladdict</media:title>
		</media:content>
	</item>
		<item>
		<title>A nice little calculator implemented in Haskell and Parsec</title>
		<link>http://haskelladdict.wordpress.com/2009/02/01/a-nice-little-calculator-implemented-in-haskell-and-parsec/</link>
		<comments>http://haskelladdict.wordpress.com/2009/02/01/a-nice-little-calculator-implemented-in-haskell-and-parsec/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 04:40:14 +0000</pubDate>
		<dc:creator>haskelladdict</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[bison]]></category>
		<category><![CDATA[calculator]]></category>
		<category><![CDATA[parsec]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[token]]></category>
		<category><![CDATA[yacc]]></category>

		<guid isPermaLink="false">http://haskelladdict.wordpress.com/?p=92</guid>
		<description><![CDATA[Last week I spent a bit of time refreshing my memory on how to write a parser using flex and bison. The standard test case was, of course, the obligatory simple calculator. This turned out to be so much fun that I decided to implement a similar calculator in Haskell using Parsec. Familiarizing myself with [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=92&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Last week I spent a bit of time refreshing my memory on how to write a parser using <em>flex </em>and <em>bison</em>. The standard test case was, of course, the obligatory simple calculator. This turned out to be so much fun that I decided to implement a similar calculator in Haskell using Parsec. Familiarizing myself with the latter had long been on my to-do list anyways. After struggling a bit with comprehending what is going on in <em>Text.ParserCombinators.Parsec.Token</em>, the resulting calculator code turned out to be quite nice (I found), concise and actually quite a bit more powerful than my original yacc/bison implementation. Here&#8217;s what I came up with:</p>
<p><pre class="brush: cpp;">
-- imports
import Text.ParserCombinators.Parsec
import qualified Text.ParserCombinators.Parsec.Token as PT
import Text.ParserCombinators.Parsec.Language (emptyDef)               

-- | main
main :: IO ()
main = do                                                              

  -- get a line from stdin
  input &lt;- getLine                                                     

  -- parse it
  case parse parse_calc &quot;&quot; input of
    Left er  -&gt; putStrLn $ &quot;Error: &quot; ++ (show er)
    Right cl -&gt; putStrLn (show cl)                                     

  main                                                                 

-- | function generating a token parser based on a
-- lexical parsers combined with a language record definition
lexer :: PT.TokenParser st
lexer  = PT.makeTokenParser emptyDef                                   

-- | token parser for parenthesis
parens :: CharParser st a -&gt; CharParser st a
parens = PT.parens lexer                                               

-- | token parser for Integer
integer :: CharParser st Integer
integer = PT.integer lexer                                             

-- | token parser for Double
float :: CharParser st Double
float = PT.float lexer

-- | token parser for Either Integer Double
naturalOrFloat :: CharParser st (Either Integer Double)
naturalOrFloat = PT.naturalOrFloat lexer                               

-- | token parser for keywords
reservedOp :: String -&gt; CharParser st ()
reservedOp = PT.reservedOp lexer                                       

-- | helper function for defining real powers
real_exp :: Double -&gt; Double -&gt; Double
real_exp a x = exp $ x * log a                                         

-- | grammar description for parser
parse_calc :: CharParser () Double
parse_calc = mul_term `chainl1` add_action                             

mul_term :: CharParser () Double
mul_term = exp_term `chainl1` multiply_action                          

exp_term :: CharParser () Double
exp_term = factor `chainl1` exp_action                                 

factor :: CharParser () Double
factor = parens parse_calc
             &lt;|&gt; parse_sqr
             &lt;|&gt; parse_number                                              

parse_sqr :: CharParser () Double
parse_sqr = reservedOp &quot;sqrt&quot; &gt;&gt; parens parse_calc &gt;&gt;=
                    \x -&gt; return $ sqrt x                                      

multiply_action :: CharParser () (Double -&gt; Double -&gt; Double)
multiply_action = do {reservedOp &quot;*&quot;; return (*) }
                            &lt;|&gt; do {reservedOp &quot;/&quot;; return (/) }                   

add_action :: CharParser () (Double -&gt; Double -&gt; Double)
add_action = do { reservedOp &quot;+&quot;; return (+) }
                      &lt;|&gt; do { reservedOp &quot;-&quot;; return (-) }                       

exp_action :: CharParser () (Double -&gt; Double -&gt; Double)
exp_action = reservedOp &quot;^&quot; &gt;&gt; return real_exp

parse_number :: CharParser () Double
parse_number = naturalOrFloat &gt;&gt;=
                            \num -&gt; case num of
                           Left i  -&gt; return $ fromInteger i
                           Right x -&gt; return x
</pre></p>
<p>The next step will be to allow for the definition of local variables that can be used during calculations. I&#8217;ll keep you posted on any progress <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>UPDATE (02/08/09): I&#8217;ve added quite a few more features to the little calculator app and named it <strong>husky</strong>. You can have a look at the current state of the code over at github (see sidebar).</p>
<p>UPDATE (04/04/09): <strong>husky </strong>has grown into a pretty decent calculator app and is pretty usable now. Check it out &#8211; the code should be pretty accessible and readable.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/haskelladdict.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/haskelladdict.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/haskelladdict.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/haskelladdict.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/haskelladdict.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/haskelladdict.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/haskelladdict.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/haskelladdict.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/haskelladdict.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/haskelladdict.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/haskelladdict.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/haskelladdict.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/haskelladdict.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/haskelladdict.wordpress.com/92/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=92&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://haskelladdict.wordpress.com/2009/02/01/a-nice-little-calculator-implemented-in-haskell-and-parsec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">haskelladdict</media:title>
		</media:content>
	</item>
		<item>
		<title>The power of the UNIX command line &#8211; part I</title>
		<link>http://haskelladdict.wordpress.com/2009/01/09/the-power-of-the-unix-command-line-part-i/</link>
		<comments>http://haskelladdict.wordpress.com/2009/01/09/the-power-of-the-unix-command-line-part-i/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 22:48:45 +0000</pubDate>
		<dc:creator>haskelladdict</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[arithmetic expansion operator]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://haskelladdict.wordpress.com/?p=82</guid>
		<description><![CDATA[Quite frequently, I notice that particularly among Unix newcomers the power of the command line seems to be somewhat forgotten. This is unfortunate, because often one can accomplish amazing things in just one or two lines of shell code. Admittedly, using the command line takes a bit of getting used to, but then it&#8217;s just [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=82&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Quite frequently, I notice that particularly among Unix newcomers the power of the command line seems to be somewhat forgotten. This is unfortunate, because often one can accomplish amazing things in just one or two lines of shell code. Admittedly, using the command line takes a bit of getting used to, but then it&#8217;s just pure fun.</p>
<p>In the following few installations I&#8217;ll give a few examples of how I leverage the command line to make my life easier. If this wets your appetite, you owe it to yourself to check out &#8220;Unix Power Tools&#8221; <a href="http://oreilly.com/catalog/9780596003302/">[1]</a> and &#8220;The Bash Cookbook&#8221; <a href="http://www.bashcookbook.com/">[2]</a>.</p>
<p>Let&#8217;s get started. Recently, I faced the following task: I had a bunch of subdirectories that each contained some simulation data files. The directories were &#8211; quite inconveniently &#8211; named iteration_50001, iteration_50002, etc. all the way up to iteration_75000.</p>
<p>Unfortunately, in order to feed them into the next step of some modeling pipeline they needed to be named iteration_1, iteration_2, ..</p>
<p>Bummer! I guess most people (including myself) wouldn&#8217;t have hesitated to throw a few manual &#8220;mv iteration_50001 iteration_1&#8243;, &#8220;mv iteration_50002 iteration_2&#8243;, etc. at their keyboard if there would have been no more than 10 files or so. But 25,000 of them! Even assuming an average of 1 s for producing each of the mv commands, this would still take about 6 hours to complete. What else could be done? Well, how about this:</p>
<pre><pre class="brush: ruby;">
#!/bin/bash
for ((new_count = 1; new_count &amp;amp;lt;= 25000; new_count++)); do
   ((current_count = new_count + 50000))
   mv iteration_${current_count} iteration_${new_count}
done
</pre></pre>
<p>This small bash script does it all and completes in just a few seconds. Let&#8217;s briefly go over it. The complete program is one big loop, incrementing the new_count variable starting at 1 all the way up to 25,000. In the second line we specify the loop condition using bash&#8217;s arithmetic expansion operator (the <em>((&#8230;))</em> expression) just like we&#8217;d do it in C or C++. The new_count variable will count our (new) target directories, i.e., using bash&#8217;s parameter substitution they will be called <em>iteration_${new_count}</em>. Next, we need to compute the counter for the directory to be moved. Luckily, this simply amounts to adding 50000 to new_count which is what line 3 is all about, again using bash&#8217;s arithmetic expansion operator. The name of the directory to be moved then is <em>iteration_${current_count}</em>. In line 4 we simply move the present directory to the new one via the mv command. That&#8217;s it!</p>
<p>Clearly, you can use a loop of this kind for many other purposes potentially saving you a lot of work and grief.</p>
<p>[1] <a href="http://oreilly.com/catalog/9780596003302/">http://oreilly.com/catalog/9780596003302/</a><br />
[2] <a href="http://www.bashcookbook.com/">http://www.bashcookbook.com/</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/haskelladdict.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/haskelladdict.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/haskelladdict.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/haskelladdict.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/haskelladdict.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/haskelladdict.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/haskelladdict.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/haskelladdict.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/haskelladdict.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/haskelladdict.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/haskelladdict.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/haskelladdict.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/haskelladdict.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/haskelladdict.wordpress.com/82/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=82&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://haskelladdict.wordpress.com/2009/01/09/the-power-of-the-unix-command-line-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">haskelladdict</media:title>
		</media:content>
	</item>
		<item>
		<title>Backing up your linux box with DAR</title>
		<link>http://haskelladdict.wordpress.com/2008/12/19/backing-up-your-linux-box-with-dar/</link>
		<comments>http://haskelladdict.wordpress.com/2008/12/19/backing-up-your-linux-box-with-dar/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 03:48:43 +0000</pubDate>
		<dc:creator>haskelladdict</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[dar]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://haskelladdict.wordpress.com/?p=56</guid>
		<description><![CDATA[More than just a handful of people unfortunately don&#8217;t do it, despite the fact that it is actually quite straightforward and painless. I am talking of course about backing up your important data! Just remember, it takes only fraction of a section to type rm -f inside the wrong directory at the wrong time. Besides, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=56&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>More than just a handful of people unfortunately don&#8217;t do it, despite the fact that it is actually quite straightforward and painless. I am talking of course about backing up your important data! Just remember, it takes only fraction of a section to type rm -f inside the wrong directory at the wrong time. Besides, hard drives are known to give up their ghost unexpectedly. Hence, backing up important data should really be as automatic and natural a task as checking ones email.</p>
<p>For Linux systems there exist a plethora of good backup solutions. Here, I will explain the use of <strong>DAR</strong> [1], which makes backing up data a breeze.</p>
<p>Before we get started, we need to decide on a certain backup strategy. One reasonable choice might be to perform a full backup initially, followed by incremental backups at certain time intervals (daily, weekly, whatever seems appropriate). Periodically, e.g., every 6 months, another full backup could be pulled, followed by another set of incrementals until the next full backup is due. If you are backing up mission critical things, daily snapshots are certainly appropriate. Otherwise, weeklies might be sufficient.</p>
<p>Next, we have to decide what files to back up and where to store them. Let&#8217;s say we want to back up all of user Susan&#8217;s home directory, /home/susan, and we will keep the backup files on a separate hard-drive (USB or whatever) mounted at /mnt/backup. We start the backup process via</p>
<p><pre class="brush: ruby;">
/usr/bin/dar -R /home/susan -c /mnt/backup/susan-home-full -z -s 1000M -P /home/susan/random-stuff
</pre></p>
<p>It is pretty easy to understand what is going on. &#8220;-R&#8221; specifies the root of the directory tree to be backed up; &#8220;-c&#8221; is the path to the backup file(s); &#8220;-z&#8221; specifies that we want to compress the backup using gzip; &#8220;-s&#8221; gives the maximum size of individual backup files (we certainly don&#8217;t want one huge 200G backup file); &#8220;-P&#8221; excludes certain sub-directories/files from the backup. Please consult [2] for many more options. That&#8217;s it, you just backed up your data!</p>
<p>In addition, it is very useful to use dar_manager supplied with dar to generate a database of all files contained in our backup. This will greatly simplify, aka, automate, retrieval of data, especially when parts of it are spread over multiple full and/or incremental backup files:</p>
<p><pre class="brush: ruby;">
# create database
/usr/bin/dar_manager -C /home/susan/dar_backup_database.dmd 

# append full backup to database
/usr/bin/dar_manager -B /home/susan/dar_backup_database.dmd -A /mnt/backup/susan-home-backup
</pre></p>
<p>Here, &#8220;-B&#8221; specifies the path to the database file and &#8220;-A&#8221; gives the path to our initial full backup.</p>
<p>Now we can simply run differential backups at whatever time intervals necessary. To do so, the following simple bash script <em>backup_home.sh </em> might be helpful:</p>
<p><pre class="brush: ruby;">
#!/bin/bash

# backup
if [ -z &quot;$1&quot; ]; then
  echo &quot;usage: backup-home-disk &lt;reference archive&gt;&quot;
  exit
fi

date=$(date +%F)

# create incremental backup
/usr/bin/dar -R /home/susan -c /mnt/backup/susan-home-diff-${date} -A &quot;$1&quot; -z -s 1000M -P  /home/susan/random-stuff

# update database
/usr/bin/dar_manager -B /home/susan/dar_backup_database.dmd -A /mnt/backup/susan-home-diff-${date}
</pre></p>
<p>Using this script, doing an incremental backup amounts to typing</p>
<p># ./backup_home.sh &lt;path to previous backup file&gt;</p>
<p>Voila!</p>
<p>Well, not so fast. Before calling it a day and feeling all &#8220;backed up&#8221;, we need to make sure that our backups work as intended. Most importantly, we need to make sure that we can actually recover our data from the backup. Here, I&#8217;ll explain how to recover a specific set of files from the backup. If you need to recover the whole archive, e.g, after a disk failure please consult [2] for a very nice step by step walk-through. Hence, let&#8217;s pretend we lost /home/susan/pics and would like to recover it from our backups. We will recover the files into /tmp/backup via</p>
<p><pre class="brush: ruby;">
# set up dar manager for recovery
/usr/bin/dar_manager -B /home/susan/dar_backup_database.dmd -o -R /tmp/restore/ -O -w

# recover data
/usr/bin/dar_manager -B /home/susan/dar_backup_database.dmd -r /home/susan/pics
</pre></p>
<p>In the initial setup step, we use the &#8220;-R&#8221; switch to specify where to recover the data to (without it we will simply replace the original files) and &#8220;-w -O&#8221; to signal that it is ok to overwrite earlier versions of files that dar_manager might pull from the backup. The second step then gets our files back via specifying all files to be recovered via the -r switch. If everything went well, you should now have a pristine copy of /home/susan/pics at the time of your last backup inside /tmp/restore. If not, please make sure to investigate what went wrong until you are absolutely sure your backups work. It certainly doesn&#8217;t hurt to occasionally check if data recovery works as it should.</p>
<p>We have only touched the surface of what <strong>DAR</strong> can do. If the above is all you need, great. If not please consult the docs for more info. As usual, <em>man dar</em> and <em>man</em> <em>dar_manager</em> are your friends.</p>
<p>[1] <a href="http://dar.linux.free.fr/">http://dar.linux.free.fr/</a><br />
[2] <a href="http://dar.linux.free.fr/doc/Tutorial.html">http://dar.linux.free.fr/doc/Tutorial.html</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/haskelladdict.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/haskelladdict.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/haskelladdict.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/haskelladdict.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/haskelladdict.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/haskelladdict.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/haskelladdict.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/haskelladdict.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/haskelladdict.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/haskelladdict.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/haskelladdict.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/haskelladdict.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/haskelladdict.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/haskelladdict.wordpress.com/56/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=56&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://haskelladdict.wordpress.com/2008/12/19/backing-up-your-linux-box-with-dar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">haskelladdict</media:title>
		</media:content>
	</item>
		<item>
		<title>I am getting tired of vi vs. emacs vs. what not rants!</title>
		<link>http://haskelladdict.wordpress.com/2008/12/07/i-am-getting-tired-of-vi-vs-emacs-vs-what-not-rants/</link>
		<comments>http://haskelladdict.wordpress.com/2008/12/07/i-am-getting-tired-of-vi-vs-emacs-vs-what-not-rants/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 00:43:11 +0000</pubDate>
		<dc:creator>haskelladdict</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[editors]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[vi]]></category>

		<guid isPermaLink="false">http://haskelladdict.wordpress.com/?p=40</guid>
		<description><![CDATA[Maybe I should not blog about this topic because it seems that way too many people get way too excited about it for reasons I don&#8217;t quite understand. Well, too late, here I am typing away. I am of course referring to the eternal vi vs. emacs vs. &#60;insert your favorite text editor here&#62; flame [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=40&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Maybe I should not blog about this topic because it seems that way too many people get way too excited about it for reasons I don&#8217;t quite understand. Well, too late, here I am typing away.</p>
<p>I am of course referring to the eternal vi vs. emacs vs. &lt;insert your favorite text editor here&gt; flame war. After reading yet another one just the other day I suddenly got really fed up with it.</p>
<p>What editor I am using during my daily typing, you ask? Well, over the past many years I have spent a considerable amount of time trying out a fair amount of different editors. At the end of the day it turned out that vim was, has been, and still is exactly the right tool for me to <strong>get stuff done</strong>. In fact, vim has become part of me to a degree that I don&#8217;t even realize its modes/keystrokes/tabs any more until I am forced to edit a document in, e.g., openoffice and find myself hitting the escape key for no apparent reason and, more annoyingly, plastering the text with i&#8217;s and j&#8217;s before reverting back to slow edit mode via the arrow keys or even (gasp) the mouse.</p>
<p>What is my point, you ask? Well, I simply think that just because of the fact that one likes editor A, editor B does not automatically suck (pardon my language). Rather, I would like to propose that anybody in need of a text editor should strive to find the one that allows them to work most productively (*). Period! If this turns out to be emacs, great; or vim, great; or kate, great. Most importantly, if all your friends think editor A is great, but after using it for some time (a few months, say) you still find yourself looking at a cheat sheet a lot, or the hot-keys just don&#8217;t seem right, maybe it is time to look again, at editor B, maybe.</p>
<p>(*) Of course, it may well be that a set of different text editors be used for certain types of tasks.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/haskelladdict.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/haskelladdict.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/haskelladdict.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/haskelladdict.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/haskelladdict.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/haskelladdict.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/haskelladdict.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/haskelladdict.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/haskelladdict.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/haskelladdict.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/haskelladdict.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/haskelladdict.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/haskelladdict.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/haskelladdict.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haskelladdict.wordpress.com&amp;blog=5767300&amp;post=40&amp;subd=haskelladdict&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://haskelladdict.wordpress.com/2008/12/07/i-am-getting-tired-of-vi-vs-emacs-vs-what-not-rants/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">haskelladdict</media:title>
		</media:content>
	</item>
	</channel>
</rss>
