<?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>Arkham's Eyrie &#187; shell</title>
	<atom:link href="http://ark.asengard.net/blog/tag/shell/feed/" rel="self" type="application/rss+xml" />
	<link>http://ark.asengard.net/blog</link>
	<description>Where Doves Kill &#38; Ravens Die.</description>
	<lastBuildDate>Tue, 06 Apr 2010 14:11:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Sed by examples, Part 2</title>
		<link>http://ark.asengard.net/blog/2009/06/29/sed-by-examples-part-2/</link>
		<comments>http://ark.asengard.net/blog/2009/06/29/sed-by-examples-part-2/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 16:19:48 +0000</pubDate>
		<dc:creator>Arkham</dc:creator>
				<category><![CDATA[computers]]></category>
		<category><![CDATA[cheatsheet]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://ark.asengard.net/blog/?p=811</guid>
		<description><![CDATA[sed (stream editor) is a Unix utility which (a) parses text files and (b) implements a programming language which can apply textual transformations to such files. It reads input files line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line. Substitution [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://ark.asengard.net/blog/wp-content/uploads/2009/06/sed_logo_white.jpg"><img class="size-medium wp-image-810 aligncenter" title="sed_logo_white" src="http://ark.asengard.net/blog/wp-content/plugins/image-shadow/cache/bb91128fdecfd0b32f4098bdc76e83c3.jpg" alt="sed_logo_white" width="300" height="213" /></a></p>
<p></p>
<blockquote><p>sed (stream editor) is a Unix utility which (a) parses text files and (b) implements a programming language which can apply textual transformations to such files.<br />
<strong>It reads input files line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line</strong>.</p></blockquote>
<p></p>
<h1>Substitution</h1>
<p></p>
<ol>
<li>
     Substitute, <strong>for every line</strong>, the first occurence of foo (if any) with spam:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># test.txt</span>
foo bar spam foo
spam foo bar egg
sausage foo spam egg
spam spam spam</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/foo/spam/'</span> test.txt</pre></div></div>

</li>
<li>
    Substitute every occurence of foo with spam:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/foo/spam/g'</span> test.txt</pre></div></div>

</li>
<li>
     Substitute every occurence of foo with spam in the first two lines:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'1,2s/foo/spam/g'</span> test.txt</pre></div></div>

</li>
<li>
    Substitute every occurence of foo with spam in every line that starts with sausage and ends with egg:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'/^sausage.*egg$/s/foo/spam/g'</span> test.txt</pre></div></div>

</li>
<li>
    Substitute paths:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s|/usr/bin/|/usr/local/bin|g'</span> script.sh</pre></div></div>

</li>
<li>
    Remove html tags:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;body&gt;
Hello World!
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/&lt;[^&gt;]*&gt;//g'</span> hello.html</pre></div></div>

</li>
</ol>
<p></p>
<h1>Advanced substitution</h1>
<p></p>
<ol>
<li>
    Append (that&#8217;s what she said) to every line ( &#038; represents what matches):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;s/^.*$/&amp; (that's what she said)/g&quot;</span> test.txt</pre></div></div>

</li>
<li>
    Append lol, rofl and lmao to the first three words of every line:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/\(^[^ ]*\) \([^ ]*\) \([^ ]*\)/\1lol \2rofl \3lmao/'</span> test.txt</pre></div></div>

</li>
</ol>
<p></p>
<h1>Multiple Commands</h1>
<p></p>
<ol>
<li>
    Print a file alternating row numbers:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'=;p'</span> test.txt</pre></div></div>

</li>
<li>
    Print some information about your cpu first core:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># script.sed</span>
<span style="color: #000000;">1</span>,<span style="color: #000000; font-weight: bold;">/</span>^$<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">/</span>model name<span style="color: #000000; font-weight: bold;">/</span>p
    <span style="color: #000000; font-weight: bold;">/</span>flags<span style="color: #000000; font-weight: bold;">/</span>p
    <span style="color: #000000; font-weight: bold;">/</span>bogomips<span style="color: #000000; font-weight: bold;">/</span>p
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-f</span> script.sed <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>cpuinfo</pre></div></div>

</li>
<li>
    Add a line before/after each line or replace line:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># script.sed</span>
i\
This line will be inserted before each line</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># script.sed</span>
a\
This line will be inserted after each line</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># script.sed</span>
c\
This line will be inserted <span style="color: #000000; font-weight: bold;">in</span> each line</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-f</span> script.sed test.txt</pre></div></div>

</li>
]]></content:encoded>
			<wfw:commentRss>http://ark.asengard.net/blog/2009/06/29/sed-by-examples-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sed by examples</title>
		<link>http://ark.asengard.net/blog/2009/06/29/sed-by-examples/</link>
		<comments>http://ark.asengard.net/blog/2009/06/29/sed-by-examples/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 23:16:40 +0000</pubDate>
		<dc:creator>Arkham</dc:creator>
				<category><![CDATA[computers]]></category>
		<category><![CDATA[cheatsheet]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://ark.asengard.net/blog/?p=801</guid>
		<description><![CDATA[sed (stream editor) is a Unix utility which (a) parses text files and (b) implements a programming language which can apply textual transformations to such files. It reads input files line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line. Delete [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://ark.asengard.net/blog/wp-content/uploads/2009/06/Canon-Tosh-SED.jpg"><img class="size-medium wp-image-802 aligncenter" title="Canon Tosh SED" src="http://ark.asengard.net/blog/wp-content/plugins/image-shadow/cache/dee3ce83e0a8cc1c23be11981c166f39.jpg" alt="Canon Tosh SED" width="300" height="224" /></a></p>
<p></p>
<blockquote><p>sed (stream editor) is a Unix utility which (a) parses text files and (b) implements a programming language which can apply textual transformations to such files.<br />
<strong>It reads input files line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line</strong>.</p></blockquote>
<p></p>
<h1>Delete</h1>
<p></p>
<ol>
<li>
    Delete the first line of a file:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'1d'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>fstab</pre></div></div>

</li>
<li>
    Delete from the second to the tenth line:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'2,10d'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>fstab</pre></div></div>

</li>
<li>
    Delete lines starting with #:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'/^#/d'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>fstab</pre></div></div>

</li>
</ol>
<p></p>
<h1>Print</h1>
<p></p>
<ol>
<li>
    Print lines starting with #:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'/^#/p'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>fstab</pre></div></div>

</li>
<li>
    Print blocks of text enclosed by BEGIN and END:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'/BEGIN/,/END/p'</span> script.awk</pre></div></div>

</li>
<li>
    Print Device section in xorg.conf:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'/Section &quot;Device&quot;/,/EndSection/p'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>X11<span style="color: #000000; font-weight: bold;">/</span>xorg.conf</pre></div></div>

</li>
<li>
    Print main function in a C source file:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'/main[[:space:]]*(/,/^}/p'</span> source.c</pre></div></div>

</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://ark.asengard.net/blog/2009/06/29/sed-by-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to colorize bash prompt easily</title>
		<link>http://ark.asengard.net/blog/2009/06/24/how-to-colorize-bash-prompt/</link>
		<comments>http://ark.asengard.net/blog/2009/06/24/how-to-colorize-bash-prompt/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 18:02:34 +0000</pubDate>
		<dc:creator>Arkham</dc:creator>
				<category><![CDATA[computers]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://ark.asengard.net/blog/?p=792</guid>
		<description><![CDATA[Sometimes, the bash prompt can look a little dull and it may become difficult to recognize where the output of a command ends. Here I provide with a very simple way to customize your bash prompt by adding some colors. First of all, add these colors definition to your .bashrc ## Fancy colors red='\[\e[0;31m\]' RED='\[\e[1;31m\]' [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://ark.asengard.net/blog/wp-content/uploads/2009/06/lost_numbers_fan.jpg"><img class="size-full wp-image-798 aligncenter" title="lost_numbers_fan" src="http://ark.asengard.net/blog/wp-content/plugins/image-shadow/cache/2f99a49f04f85db277424a9aeeac11df.jpg" alt="lost_numbers_fan" width="465" height="313" /></a></p>
<p>Sometimes, the bash prompt can look a little dull and it may become difficult to recognize where the output of a command ends. Here I provide with a very simple way to customize your bash prompt by adding some colors.</p>
<p>First of all, add these colors definition to your .bashrc</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">## Fancy colors</span>
<span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">red</span></span>=<span style="color: #ff0000;">'\[\e[0;31m\]'</span>
<span style="color: #007800;">RED</span>=<span style="color: #ff0000;">'\[\e[1;31m\]'</span>
<span style="color: #007800;">blue</span>=<span style="color: #ff0000;">'\[\e[0;34m\]'</span>
<span style="color: #007800;">BLUE</span>=<span style="color: #ff0000;">'\[\e[1;34m\]'</span>
<span style="color: #007800;">cyan</span>=<span style="color: #ff0000;">'\[\e[0;36m\]'</span>
<span style="color: #007800;">CYAN</span>=<span style="color: #ff0000;">'\[\e[1;36m\]'</span>
<span style="color: #007800;">black</span>=<span style="color: #ff0000;">'\[\e[0;30m\]'</span>
<span style="color: #007800;">BLACK</span>=<span style="color: #ff0000;">'\[\e[1;30m\]'</span>
<span style="color: #007800;">green</span>=<span style="color: #ff0000;">'\[\e[0;32m\]'</span>
<span style="color: #007800;">GREEN</span>=<span style="color: #ff0000;">'\[\e[1;32m\]'</span>
<span style="color: #007800;">yellow</span>=<span style="color: #ff0000;">'\[\e[0;33m\]'</span>
<span style="color: #007800;">YELLOW</span>=<span style="color: #ff0000;">'\[\e[1;33m\]'</span>
<span style="color: #007800;">magenta</span>=<span style="color: #ff0000;">'\[\e[0;35m\]'</span>
<span style="color: #007800;">MAGENTA</span>=<span style="color: #ff0000;">'\[\e[1;35m\]'</span>
<span style="color: #007800;">white</span>=<span style="color: #ff0000;">'\[\e[0;37m\]'</span>
<span style="color: #007800;">WHITE</span>=<span style="color: #ff0000;">'\[\e[1;37m\]'</span>
<span style="color: #007800;">NC</span>=<span style="color: #ff0000;">'\[\e[0m\]'</span> <span style="color: #666666; font-style: italic;"># No Color</span></pre></div></div>

<p>The variables you want to use are</p>
<ul>
<li>\u &#8211; username</li>
<li>\h &#8211; host name</li>
<li>\w &#8211; current absolute path</li>
<li>\W for current relative path</li>
<li>\$ &#8211; te prompt character (eg. &#8216;#&#8217;)</li>
</ul>
<p>For example, my current setup looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">PS1</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${green}</span>\u<span style="color: #007800;">${NC}</span>@<span style="color: #007800;">${green}</span>\h <span style="color: #007800;">${BLUE}</span>\w<span style="color: #007800;">${NC}</span> <span style="color: #007800;">${GREEN}</span><span style="color: #000099; font-weight: bold;">\$</span> <span style="color: #007800;">${NC}</span> &quot;</span></pre></div></div>

<p>Enjoy <img src='http://ark.asengard.net/blog/wp-content/plugins/smilies-themer/tango/face-wink.png' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ark.asengard.net/blog/2009/06/24/how-to-colorize-bash-prompt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Awk by examples</title>
		<link>http://ark.asengard.net/blog/2009/04/14/awk-by-examples/</link>
		<comments>http://ark.asengard.net/blog/2009/04/14/awk-by-examples/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 14:27:40 +0000</pubDate>
		<dc:creator>Arkham</dc:creator>
				<category><![CDATA[computers]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[cheatsheet]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://ark.asengard.net/blog/?p=661</guid>
		<description><![CDATA[AWK is a language for processing files of text. A file is treated as a sequence of records, and by default each line is a record. Each line is broken up into a sequence of fields, so we can think of the first word in a line as the first field, the second word as [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img src="http://ark.asengard.net/blog/wp-content/uploads/2009/04/auk-249x300.png" alt="auk" title="auk" width="249" height="300" class="alignnone size-medium wp-image-710" /></p>
<p></p>
<blockquote><p>AWK is a language for processing files of text. A file is treated as a sequence of records, and by default each line is a record. Each line is broken up into a sequence of fields, so we can think of the first word in a line as the first field, the second word as the second field, and so on. An AWK program is of a sequence of pattern-action statements. AWK reads the input a line at a time. A line is scanned for each pattern in the program, and for each pattern that matches, the associated action is executed.</p></blockquote>
<p></p>
<h1>Basics</h1>
<p></p>
<ol>
<li>
    Print /etc/mtab (awk reads a line at time and prints it) :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{ print }'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mtab</pre></div></div>

</li>
<li>
    Print /etc/mtab ($0 denotes the whole line) :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{ print $0 }'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mtab</pre></div></div>

</li>
<li>
    List mounted filesystems ($1 denotes the first element of the line) :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{ print $1 }'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mtab</pre></div></div>

</li>
<li>
    List groups (-F chooses the Field Separator) :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span><span style="color: #ff0000;">&quot;:&quot;</span> <span style="color: #ff0000;">'{ print $1 }'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>group</pre></div></div>

</li>
<li>
    List groups and id (awk concatenates print() arguments) :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span><span style="color: #ff0000;">&quot;:&quot;</span> <span style="color: #ff0000;">'{ print $1 &quot; &quot; $3 }'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>group</pre></div></div>

</li>
<li>
    List groups and id (Nicer format) :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span><span style="color: #ff0000;">&quot;:&quot;</span> <span style="color: #ff0000;">'{ print &quot;group: &quot; $1 &quot;\tid: &quot; $3 }'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>group</pre></div></div>

</li>
<li>
    Launching external awk scripts :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># first.awk</span>
BEGIN <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #007800;">FS</span>=<span style="color: #ff0000;">&quot;:&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span> print $<span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-f</span> first.awk <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mtab</pre></div></div>

</li>
<li>
    List IPv4 addressess :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ifconfig</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'/inet / { print $2 }'</span></pre></div></div>

</li>
<li>
    List processes run by root :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># psroot.awk</span>
$<span style="color: #000000;">1</span> == <span style="color: #ff0000;">&quot;root&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> 
    <span style="color: #7a0874; font-weight: bold;">printf</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;ROOT: &quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; 
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">i</span>=<span style="color: #000000;">11</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>=NF; i++<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
        <span style="color: #7a0874; font-weight: bold;">printf</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;%s &quot;</span>, <span style="color: #007800;">$i</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
    <span style="color: #7a0874; font-weight: bold;">printf</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ps</span> au <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-f</span> psroot.awk</pre></div></div>

</li>
<li>
    Print X warnings :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'$1 ~ /(WW)/ { print }'</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>Xorg.0.log</pre></div></div>

</li>
<li>
    Print a file removing comments :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'! /^#/ { print }'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>fstab</pre></div></div>

</li>
<li>
    Print number of files/directories :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-lA</span> <span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'BEGIN{ x=0 } { x=x+1 } END{ print x-1 }'</span></pre></div></div>

</li>
<li>
    Awk as calculator :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{ print ((2*5^2-1)%7) }'</span></pre></div></div>

</li>
<li>
    Count empty lines :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># blanklines.awk</span>
BEGIN <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color: #007800;">x</span>=<span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span> 
<span style="color: #000000; font-weight: bold;">/</span>^$<span style="color: #000000; font-weight: bold;">/</span>  <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color: #007800;">x</span>=x+<span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span> 
END   <span style="color: #7a0874; font-weight: bold;">&#123;</span> print <span style="color: #ff0000;">&quot;I found &quot;</span> x <span style="color: #ff0000;">&quot; blank lines :)&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-f</span> blanklines.awk <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>profile</pre></div></div>

</li>
<li>
    Use regexp as FS (note the difference though) :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'   a   b c  d '</span><span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span><span style="color: #ff0000;">&quot;[ <span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\n</span>]+&quot;</span> <span style="color: #ff0000;">'{ print $2 }'</span></pre></div></div>


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

</li>
<li>
    Understand NR and NF :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># nrnf.awk</span>
BEGIN <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color: #007800;">x</span>=<span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span> 
      <span style="color: #7a0874; font-weight: bold;">&#123;</span> print <span style="color: #ff0000;">&quot;Words on line &quot;</span> NR <span style="color: #ff0000;">&quot;: &quot;</span> NF ; x+=NF <span style="color: #7a0874; font-weight: bold;">&#125;</span> 
END   <span style="color: #7a0874; font-weight: bold;">&#123;</span> print <span style="color: #ff0000;">&quot;Total lines: &quot;</span> NR ; print <span style="color: #ff0000;">&quot;Total words: &quot;</span> x <span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-f</span> nrnf.awk nrnf.awk</pre></div></div>

</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://ark.asengard.net/blog/2009/04/14/awk-by-examples/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
