<?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>The Inferno &#187; utility</title>
	<atom:link href="http://www.viren.ca/blog/category/utility/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.viren.ca/blog</link>
	<description>It is a fallacy to state that something exists just because it can't be proven that it doesn't</description>
	<lastBuildDate>Fri, 30 Jul 2010 03:49:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Just how long again?</title>
		<link>http://www.viren.ca/blog/2010/02/just-how-long-again/</link>
		<comments>http://www.viren.ca/blog/2010/02/just-how-long-again/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 05:46:29 +0000</pubDate>
		<dc:creator>Viren</dc:creator>
				<category><![CDATA[awk]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[bash script]]></category>
		<category><![CDATA[text processing]]></category>
		<category><![CDATA[utility]]></category>
		<category><![CDATA[discography duration]]></category>
		<category><![CDATA[exiftool]]></category>
		<category><![CDATA[length]]></category>
		<category><![CDATA[mp3]]></category>

		<guid isPermaLink="false">http://www.viren.ca/blog/?p=617</guid>
		<description><![CDATA[
If you&#8217;ve ever wanted to see how long you&#8217;ll be playing a particular artist&#8217;s music for, if you were to play their entire discography, I present a one-liner in bash that will show you just that.

find . -type f -name '*.mp*' -exec exiftool '{}' + &#124; grep Duration &#124; awk '{x += $3; print x;}'

The [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="aligncenter" src="http://www.qwantz.com/comics/comic2-1205.png" alt="" width="735" height="500" /></p>
<p>If you&#8217;ve ever wanted to see how long you&#8217;ll be playing a particular artist&#8217;s music for, if you were to play their entire discography, I present a one-liner in bash that will show you just that.</p>
<blockquote>
<pre>find . -type f -name '*.mp*' -exec exiftool '{}' + | grep Duration | awk '{x += $3; print x;}'</pre>
</blockquote>
<p>The venerable <em>find</em> command needs no introduction. Suffice it to say that the type switch restricts it to files, and the *.mp* restricts the files found to MP3s or MP2s.</p>
<p><em>Exiftool</em> is a nifty command-line processing tool for tags of all kinds, as seen by its name. In this case, we want just the &#8216;Duration&#8217; field of each song. Once we have those, we pipe those to <em>awk</em> and get a running total, which shows us how long the entire discography is when the final total is printed.</p>
<p>Now I know that I have 5668.14 minutes of Zappa goodness, or a mere 208 minutes of godly Death. You need to run this in the folder that has all the albums by the artist, or of course, you can adapt it to a script and pass in parameters and so on.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.viren.ca/blog/2010/02/just-how-long-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>She sells Shell Scripts on the Sea Shore</title>
		<link>http://www.viren.ca/blog/2008/08/she-sells-shell-scripts-on-the-sea-shore/</link>
		<comments>http://www.viren.ca/blog/2008/08/she-sells-shell-scripts-on-the-sea-shore/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 07:02:52 +0000</pubDate>
		<dc:creator>Viren</dc:creator>
				<category><![CDATA[awk]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[bash script]]></category>
		<category><![CDATA[shell script]]></category>
		<category><![CDATA[text processing]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://www.viren.ca/blog/?p=242</guid>
		<description><![CDATA[Recently, I had to parse data in several text files and calculate averages. From these averages, I had to create a chart. So I could either write a program using a real language like C or Perl or something, or even worse, copy and paste each value into a spreadsheet and then go from there. [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I had to parse data in several text files and calculate averages. From these averages, I had to create a chart. So I could either write a program using a real language like C or Perl or something, or even worse, copy and paste each value into a spreadsheet and then go from there. You should be shuddering by now.</p>
<p>The data was in 5 different folders, with each folder containing 25 files, with the contents of each file being:</p>
<blockquote><p>real 70.67<br />
user 70.66<br />
sys 0.00<br />
real 70.82<br />
user 70.81<br />
sys 0.01<br />
real 70.89<br />
user 70.88<br />
sys 0.00</p></blockquote>
<p>What I needed was the average of the three lines with the word &#8220;real&#8221; in them. So, first we grep through all the files for the word real to get something like:</p>
<blockquote><p>real 70.67<br />
real 70.82<br />
real 70.89<br />
real 41.27<br />
real 41.16<br />
real 41.39<br />
real 125.75<br />
real 125.42</p></blockquote>
<p>Now, we need to sum up every 3 lines and divide by 3 to get the average. Enter awk:</p>
<blockquote><p>awk  &#8216;{x+=$2;if(!(NR%3)){printf(&#8220;%2.3f\n&#8221;,x/3);x=0}}&#8217;</p></blockquote>
<p>What this does is to add the second column ($2) to a variable called x. Awk automatically initializes all variables to zero, so we don&#8217;t need to worry about bogus data. The NR variable holds the number of lines, so every time we pass three lines, divide the current sum by 3 and then reset the subtotal to zero. Perhaps, making the code a bit tidier might help, even though everyone loves those cryptic one liners:</p>
<blockquote><p>awk  &#8216;{</p>
<p>x+=$2;</p>
<p>if(!(NR%3)){</p>
<p>printf(&#8220;%2.3f\n&#8221;,x/3);</p>
<p>x=0}</p>
<p>}&#8217;</p></blockquote>
<p>Why, it&#8217;s almost C, I can hear you say.</p>
<p>Now, we stitch them together into one glorious command:</p>
<blockquote><p>cat 2.30GHz/* | grep real | awk  &#8216;{x+=$2;if(!(NR%3)){printf(&#8220;%2.3f\n&#8221;,x/3);x=0}}&#8217;</p></blockquote>
<p>We need to replace the 2.30GHz by a variable, so we can iterate through the folders. And we need to append the output to a file, to be imported into your favorite spreadsheet later. Here&#8217;s the final script</p>
<blockquote><p>#!/bin/bash</p>
<p>freqs=( 2.30GHz 2.00GHz 1.70GHz 1.40GHz 1.15GHz )</p>
<p>for l in ${freqs[@]}<br />
do<br />
data=`cat $l/* | grep real | awk  &#8216;{x+=$2;if(!(NR%3)){printf(&#8220;%2.3f\n&#8221;,x/3);x=0}}&#8217;`<br />
echo $data &gt;&gt; file.csv<br />
done</p></blockquote>
<p>And file.csv of course looks like:</p>
<blockquote><p>198.967 265.083 543.800 139.247 51.973 70.793 41.273 125.640 214.127 220.863 91.303 15.230 46.397 256.093 176.000 178.037 213.133 183.947 31.743 181.223 220.143 192.857 47.360 82.017 177.790<br />
224.363 295.043 601.253 150.720 58.730 81.213 47.243 132.723 244.263 244.343 103.090 17.160 53.183 273.963 188.223 201.857 238.343 209.247 36.773 207.530 235.840 223.670 53.480 91.600 196.297<br />
256.523 316.210 676.660 165.870 67.860 95.867 55.793 138.647 279.900 273.450 118.363 19.590 62.377 293.080 199.717 236.997 271.997 242.750 42.667 244.317 253.797 259.940 61.300 102.497 220.560</p>
<p>&#8230;..</p></blockquote>
<p>There you have it. Averages of all the required numbers from every file, all in one file. Import it as a space delimited file into Calc or Excel and Robert&#8217;s your mother&#8217;s brother.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.viren.ca/blog/2008/08/she-sells-shell-scripts-on-the-sea-shore/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.313 seconds -->
