<?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>No Znx! &#187; cli</title>
	<atom:link href="http://znx.no/tag/cli/feed/" rel="self" type="application/rss+xml" />
	<link>http://znx.no</link>
	<description>the pigeons!!!!</description>
	<lastBuildDate>Tue, 20 Dec 2011 22:42:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Skinning A Cat</title>
		<link>http://znx.no/2009/05/skinning-a-cat/</link>
		<comments>http://znx.no/2009/05/skinning-a-cat/#comments</comments>
		<pubDate>Sun, 17 May 2009 02:02:36 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://znx.no/?p=116</guid>
		<description><![CDATA[I was recently discussing with someone about sed usage. They were having difficultly creating an appropriate regex to handle their problem: (Orlando) sed regex kicks my ass. I like to remove the second : in the line. So that in 123:456:6789 it will only returns 123:456. I can find the first :, but I have [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently discussing with someone about <code>sed</code> usage. They were having difficultly creating an appropriate regex to handle their problem:</p>
<pre>(Orlando) sed regex kicks my ass.  I like to remove the second : in the
line. So that in 123:456:6789 it will only returns 123:456.  I can find the
first :, but I have not been able to use s/:{2}// to find the second
one, and remove the rest.</pre>
<p>I enjoy regex (I know I&#8217;m weird, leave me alone), so I was able to provide an answer to this problem:</p>
<pre>(@znx) Orlando: the trick is to use [^:] and \1 ..
(@znx) like:   s/\(:[^:]*\):.*/\1/</pre>
<p>Now obviously at first glance this regex could be a bit intimating to someone who is still picking up the skills but as with most things if we break it down it becomes easier.</p>
<p>Working out to in, <code>\(    \):.*</code>, that says match something with <strong>:</strong> at the end and all the character after it. The <strong>.</strong> is a special meaning &#8220;any character&#8221; and <strong>*</strong> to match multiple characters. The first match will be stored by <code>sed</code> and assigned into the <strong>\1</strong> for the replacement (that is what the brackets do). Inside the brackets we have <code>:[^:]*</code>. The sequence <strong>[^ ]</strong> is a negated list, that means that we are asking it to match everything that is NOT inside the list, in this case <strong>:</strong>.</p>
<p>Putting it altogether we are saying: Match a leading : and a trailing : with any characters after it. Placing the contents between the two <strong>:</strong> in memory. Then finally we replace the contents.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">123</span>:<span style="color: #000000;">456</span>:<span style="color: #000000;">6789</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/\(:[^:]*\):.*/\1/'</span>
<span style="color: #000000;">123</span>:<span style="color: #000000;">456</span></pre></div></div>

<p>Sucess, however as with most things, there is more than one way to skin a cat and regex is rarely the prettiest method. So what other ways can we solve this problem?</p>
<p>With AWK:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">123</span>:<span style="color: #000000;">456</span>:<span style="color: #000000;">6789</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F: <span style="color: #ff0000;">'{print $1&quot;:&quot;$2}'</span>
<span style="color: #000000;">123</span>:<span style="color: #000000;">456</span></pre></div></div>

<p>With <code>cut</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">123</span>:<span style="color: #000000;">456</span>:<span style="color: #000000;">6789</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> -d: -f1,<span style="color: #000000;">2</span>
<span style="color: #000000;">123</span>:<span style="color: #000000;">456</span></pre></div></div>

<p>As always, experimentation with the mass of GNU tools you can find on your system will bring a greater deal of power to your tool chest. Mind you, then I wouldn&#8217;t get complements for helping would I?</p>
<pre>(Orlando) Wow, When I grow up, I like to remember this thing like you do. <img src='http://znx.no/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </pre>
<p>Haha, till next time!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/05/skinning-a-cat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

