<?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; awk</title>
	<atom:link href="http://ark.asengard.net/blog/tag/awk/feed/" rel="self" type="application/rss+xml" />
	<link>http://ark.asengard.net/blog</link>
	<description>Where Doves Kill &#38; Ravens Die.</description>
	<lastBuildDate>Fri, 28 Oct 2011 09:09:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<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: #007800;">$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: #007800;">$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>

