<?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>Clker.com - weblog &#187; awk</title>
	<atom:link href="http://www.clker.com/blog/tag/awk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.clker.com/blog</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 22 Feb 2010 19:21:27 +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>Discovering some of Linux Cow Powers: awk and bash</title>
		<link>http://www.clker.com/blog/2008/07/22/discovering-some-of-linux-cow-powers-awk-and-bash/</link>
		<comments>http://www.clker.com/blog/2008/07/22/discovering-some-of-linux-cow-powers-awk-and-bash/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 05:10:35 +0000</pubDate>
		<dc:creator>Mohamed Ibrahim</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[less]]></category>
		<category><![CDATA[move]]></category>
		<category><![CDATA[mv]]></category>
		<category><![CDATA[rename]]></category>

		<guid isPermaLink="false">http://www.clker.com/blog/?p=114</guid>
		<description><![CDATA[If you&#8217;ve never used linux you should give it a try. The simplest way is to install colinux with xming. If you feel its involved, just burn an ubuntu 8.04 bootable cd. It works out of the CD with almost all the every day used features without installtion, formating or any other extra work.
One of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.clker.com/clipart-3716.html" target="_blank"><img style="border:0px;margin:5px;float:left;" title="Etiquette Icons" src="http://www.clker.com/cliparts/b/6/c/b/11949857311509536676gnome-terminal.svg.thumb.png" alt="" /></a>If you&#8217;ve never used linux you should give it a try. The simplest way is to install colinux with xming. If you feel its involved, just burn an ubuntu 8.04 bootable cd. It works out of the CD with almost all the every day used features without installtion, formating or any other extra work.</p>
<p>One of the cow powers that I use linux for, and didn&#8217;t find a way to do it using the windows shell is renaming a lot of files. I had around 3000 bmp files, their names were in the following format:</p>
<p>&lt;word&gt;-&lt;volume&gt;-&lt;number&gt;.bmp</p>
<p>and I wanted to rename them to</p>
<p>&lt;word&gt;-&lt;number&gt;.bmp</p>
<p>I couldn&#8217;t find a way to do this on windows except with writing a small pogram. On linux the solution is a program called <strong>awk</strong>, which can tokenize input strings, and write parts of them out. <strong>awk</strong> is a sophistiacated tool, and I usually use a very small subset of its features.</p>
<p>The solution was finally something similar to this:</p>
<pre>find .bmp | awk -F- '{print "mv "$0" "$1"-"$3;}' | bash -v</pre>
<p>The first part is running the find program to find all files with extension .bmp. The results are then piped as input to awk, instead of being written to the screen. <strong>awk</strong> is told that the field separator is &#8216;-&#8217; using the -F parameter. A one statement <strong>awk</strong> program says print the words <strong>mv</strong> to issue commands to rename a file, then <strong>$0</strong> for the full string before tokenizing, then the <strong>$1</strong> for the first tokenized part, and <strong>$3</strong> for the third tokenized part, skipping the second. So <strong>awk</strong> is being used to write the commands that we need to execute.</p>
<p>Those commands are then piped to <strong>bash</strong>, the bourne shell with the verbose flag on so that we can see the commands being executed.</p>
<p>A good practice when trying something like that is to pipe the results first to <strong>less</strong>, instead of <strong>bash</strong>. <strong>less</strong> is a program like <strong>more</strong>, which gives the user the ability to inspect whatever was piped to it and filp forward and backwords. It also includes some nice features like -N to show numbers, and searching for a word using the forward slash /  , with commands very similary to vim.</p>
<p>So the less version of the line whould be this:</p>
<pre>find .bmp | awk -F- '{print "mv "$0" "$1"-"$3;}' | less</pre>
<p>This is a very handy technique in renaming lots of files, or carrying batch operations on multiple files that I usually use.</p>
<p>Technorati Tags: <a href="#" rel="tag">linux</a>, <a href="#" rel="tag"> bash</a>, <a href="#" rel="tag"> find</a>, <a href="#" rel="tag"> awk</a>, <a href="#" rel="tag"> less</a>, <a href="#" rel="tag"> rename</a>, <a href="#" rel="tag"> move</a>, <a href="#" rel="tag"> mv</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.clker.com/blog/2008/07/22/discovering-some-of-linux-cow-powers-awk-and-bash/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
