<?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>thirstymind.org &#187; bash</title>
	<atom:link href="http://www.thirstymind.org/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thirstymind.org</link>
	<description>andrew watts' weblog</description>
	<lastBuildDate>Sat, 17 Oct 2009 22:11:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Checking the Stock Market from the Command Line</title>
		<link>http://www.thirstymind.org/2008/10/08/checking-the-stock-market-from-the-command-line/</link>
		<comments>http://www.thirstymind.org/2008/10/08/checking-the-stock-market-from-the-command-line/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 01:51:57 +0000</pubDate>
		<dc:creator>Andrew Watts</dc:creator>
				<category><![CDATA[how to]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[market]]></category>
		<category><![CDATA[stocks]]></category>

		<guid isPermaLink="false">http://www.thirstymind.org/?p=402</guid>
		<description><![CDATA[With all the financial trouble in the country I have been checking the market a lot lately, and I find it a little annoying to constantly switch to my web browser and refresh or check my dashboard or whatever.  Since I spend a lot of time in a terminal, I put a function together, [...]]]></description>
			<content:encoded><![CDATA[<p>With all the financial trouble in the country I have been checking the market a lot lately, and I find it a little annoying to constantly switch to my web browser and refresh or check my dashboard or whatever.  Since I spend a lot of time in a terminal, I put a function together, which will always display the latest market conditions for the Dow Jones Industrial Average, the Nasdaq and the S&#038;P 500 as well as any stocks you feed the function.</p>
<p>For example:</p>
<div class="geshi no term">
<ol>
<li class="li1">
<div class="de1">$ market
</div>
</li>
<li class="li1">
<div class="de1">Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Price &nbsp; &nbsp; Change &nbsp; Pct &nbsp; &nbsp; Day Lo &nbsp; &nbsp;Day Hi &nbsp;
</div>
</li>
<li class="li1">
<div class="de1">Dow Jones Industr &nbsp; &nbsp; &nbsp;9258.10 &nbsp;-189.01 &nbsp;-2.00% &nbsp; 9194.78 &nbsp; 9628.07
</div>
</li>
<li class="li1">
<div class="de1">NASDAQ COMPOSITE &nbsp; &nbsp; &nbsp; 1740.33 &nbsp; -14.55 &nbsp;-0.83% &nbsp; 1706.86 &nbsp; 1806.89
</div>
</li>
<li class="li1">
<div class="de1">S&amp;P 500 INDEX,RTH &nbsp; &nbsp; &nbsp; 984.94 &nbsp; -11.29 &nbsp;-1.13% &nbsp; &nbsp;970.97 &nbsp; 1021.06</div>
</li>
</ol>
</div>
<p>or</p>
<div class="geshi no term">
<ol>
<li class="li1">
<div class="de1">$ market GE XOM
</div>
</li>
<li class="li1">
<div class="de1">Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Price &nbsp; &nbsp; Change &nbsp; Pct &nbsp; &nbsp; Day Lo &nbsp; &nbsp;Day Hi &nbsp;
</div>
</li>
<li class="li1">
<div class="de1">Dow Jones Industr &nbsp; &nbsp; &nbsp;9258.10 &nbsp;-189.01 &nbsp;-2.00% &nbsp; 9194.78 &nbsp; 9628.07
</div>
</li>
<li class="li1">
<div class="de1">NASDAQ COMPOSITE &nbsp; &nbsp; &nbsp; 1740.33 &nbsp; -14.55 &nbsp;-0.83% &nbsp; 1706.86 &nbsp; 1806.89
</div>
</li>
<li class="li1">
<div class="de1">S&amp;P 500 INDEX,RTH &nbsp; &nbsp; &nbsp; 984.94 &nbsp; -11.29 &nbsp;-1.13% &nbsp; &nbsp;970.97 &nbsp; 1021.06
</div>
</li>
<li class="li1">
<div class="de1">GEN ELECTRIC CO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;20.65 &nbsp; &nbsp; 0.35 &nbsp;+1.72% &nbsp; &nbsp; 19.90 &nbsp; &nbsp; 21.99
</div>
</li>
<li class="li1">
<div class="de1">EXXON MOBIL CP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 77.00 &nbsp; &nbsp; 0.93 &nbsp;+1.22% &nbsp; &nbsp; 74.00 &nbsp; &nbsp; 79.39</div>
</li>
</ol>
</div>
<p>The key to making this work is a csv generated by a http request to Yahoo.  The request takes 2 arguments, the stocks and a string of tags.</p>
<p>For Example:<br />
<a href="http://download.finance.yahoo.com/d/quotes.csv?s=^DJI+GE&#038;f=nk1k2m2">http://download.finance.yahoo.com/d/quotes.csv?s=^DJI+GE&#038;f=nk1k2m2</a></p>
<p>Which says grab the name, last trade price, change percent, and the days range (all real time) for all the stocks defined by s.  There is a whole slew of tags that you can grab, the best resource of this information that I found is located at <a href="http://www.gummy-stuff.org/Yahoo-data.htm">Gummy Stuff&#8217;s Yahoo Data page</a>.</p>
<p>After some magic with sed and awk you get the above result.</p>
<p>And finally Here is the code, if anyone wants to add it to their bash profile.</p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">function</span> market <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re2">stocks=</span><span class="st0">&quot;^DJI+^IXIC+^GSPC&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">for</span> arg <span class="kw1">in</span> <span class="st0">&quot;$@&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">do</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; stocks+=<span class="st0">&quot;+${arg}&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">done</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co0"># Note the sed commands below do the following:</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co0"># &nbsp; 1. Strip any html</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co0"># &nbsp; 2. Replace the &#39;&quot;,&quot;&#39; delimeter with a #</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co0"># &nbsp; 3. Replace the &#39; &#8211; &#39; delimeter with a #</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co0"># &nbsp; 4. Strip the leading and trailing &quot; characters</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co0"># The awk command does some pretty printing on each record with # used for a delimeter</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; curl -s <span class="st0">&quot;http://download.finance.yahoo.com/d/quotes.csv?s=$stocks&amp;f=nk1k2m2&quot;</span> <span class="sy0">|</span> \</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">sed</span> <span class="st0">&#39;s/&lt; [^&gt;]*&gt;//g&#39;</span> <span class="sy0">|</span> \</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">sed</span> <span class="st0">&#39;s/&quot;,&quot;/#/g&#39;</span> <span class="sy0">|</span> \</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">sed</span> <span class="st0">&#39;s/ &#8211; /#/g&#39;</span> <span class="sy0">|</span> \</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">sed</span> <span class="st0">&#39;s/&quot;<span class="es0">\(</span>.*<span class="es0">\)</span>&quot;/<span class="es0">\1</span>/&#39;</span> <span class="sy0">|</span> \</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">awk</span> <span class="st0">&#39;BEGIN{FS=&quot;#&quot;; printf(&quot;%-20s &nbsp;%-8s &nbsp;%-7s &nbsp;%-6s &nbsp;%-8s &nbsp;%-9s<span class="es0">\n</span>&quot;, &quot;Name&quot;, &quot;Price&quot;, &quot;Change&quot;, &quot;Pct&quot;, &quot;Day Lo&quot;, &quot;Day Hi&quot;)} { printf(&quot;%-20s &nbsp;%8s &nbsp;%7s &nbsp;%6s &nbsp;%8s &nbsp;%9s<span class="es0">\n</span>&quot;, $1, $3, $4, $5, $6, $7) }&#39;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>I think some of the sed commands could be improved to reduce the number of them, but this is simple and works.  So I&#8217;m happy for now, or would be if I started see some numbers from the economy that looked better.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thirstymind.org/2008/10/08/checking-the-stock-market-from-the-command-line/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Handling whitespace in bash for loops</title>
		<link>http://www.thirstymind.org/2008/06/27/handling-whitespace-in-bash-for-loops/</link>
		<comments>http://www.thirstymind.org/2008/06/27/handling-whitespace-in-bash-for-loops/#comments</comments>
		<pubDate>Sat, 28 Jun 2008 03:31:24 +0000</pubDate>
		<dc:creator>Andrew Watts</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[macports]]></category>

		<guid isPermaLink="false">http://www.thirstymind.org/?p=366</guid>
		<description><![CDATA[Tonight I was trying to cleanup my macports installation by uninstalling the packages that are no longer active.  I decided to write a script to do this, instead of manually uninstalling them one by one.  But in writing the script I came across something I had never seen and the google didn&#8217;t give [...]]]></description>
			<content:encoded><![CDATA[<p>Tonight I was trying to cleanup my <a href="http://www.macports.org/">macports</a> installation by uninstalling the packages that are no longer active.  I decided to write a script to do this, instead of manually uninstalling them one by one.  But in writing the script I came across something I had never seen and the google didn&#8217;t give me a clear answer so I thought I would share.</p>
<p>A typical for loop might look like:</p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1">$ <span class="kw1">for</span> <span class="kw2">file</span> <span class="kw1">in</span> `<span class="kw2">ls</span>`</div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> <span class="kw1">do</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> &nbsp; &nbsp; <span class="kw3">echo</span> <span class="re1">$file</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> <span class="kw1">done</span></div>
</li>
</ol>
</div>
<p>And this assumes there are no spaces in any of the files returned by ls.  I did not know this.  So I started doing something similar with this:</p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1">$ <span class="kw1">for</span> package <span class="kw1">in</span> `port installed <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st0">&#39;NF==2{print $0}&#39;</span> `</div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> <span class="kw1">do</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> &nbsp; &nbsp; <span class="kw3">echo</span> <span class="re1">$package</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> <span class="kw1">done</span></div>
</li>
</ol>
</div>
<p>But I got output that looked like this:</p>
<div class="geshi no none">
<ol>
<li class="li1">
<div class="de1">curl
</div>
</li>
<li class="li1">
<div class="de1">@7.17.1_0
</div>
</li>
<li class="li1">
<div class="de1">gettext
</div>
</li>
<li class="li1">
<div class="de1">@0.17_2</div>
</li>
</ol>
</div>
<p>And wanted output like this:</p>
<div class="geshi no none">
<ol>
<li class="li1">
<div class="de1">curl @7.17.1_0
</div>
</li>
<li class="li1">
<div class="de1">gettext @0.17_2</div>
</li>
</ol>
</div>
<p>The problem is the awk output had spaces in it which were split by the for loop processing.  And well to handle the spaces as desired you need to pipe the output to a while loop like so:</p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1">$ port installed <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st0">&#39;NF==2{print $0}&#39;</span> <span class="sy0">|</span> <span class="kw1">while</span> <span class="kw2">read</span> pakage</div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> <span class="kw1">do</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> &nbsp; &nbsp; <span class="kw2">sudo</span> port uninstall <span class="re1">$package</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> <span class="kw1">done</span></div>
</li>
</ol>
</div>
<p>And now $package is what I expect including the spaces, and my macports installation is a little cleaner.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thirstymind.org/2008/06/27/handling-whitespace-in-bash-for-loops/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
