<h1 id="gnu-awk---part-6">Gnu Awk - Part 6</h1>
<h2 id="introduction">Introduction</h2>
<p>This is the sixth episode of the “<a href="https://hackerpublicradio.org/series/0094.html" title="Learning Awk">Learning Awk</a>” series that <a href="https://hackerpublicradio.org/correspondents/0300.html" title="b-yeezi">b-yeezi</a> and I are doing.</p>
<h2 id="recap-of-the-last-episode">Recap of the last episode</h2>
<h3 id="regular-expressions">Regular expressions</h3>
<p>In the last episode we saw regular expressions in the ‘<em>pattern</em>’ part of a ‘<em>pattern {action}</em>’ sequence. Such a sequence is called a ‘<em>RULE</em>’, (as we have seen in earlier episodes).</p>
<pre><code>$1 ~ /p[elu]/ {print $0}</code></pre>
<p><strong>Meaning</strong>: <em>If field 1 contains a ‘p’ followed by one of ‘e’, ‘l’ or ‘u’ print the whole line</em>.</p>
<pre><code>$2 ~ /e{2}/ {print $0}</code></pre>
<p><strong>Meaning</strong>: <em>If field 2 contains two instances of letter ‘e’ in sequence, print the whole line.</em></p>
<p>It is usual to enclose the regular expression in slashes, which make it a <em>regexp constant</em>.</p>
<p>We had a look at many of the <em>operators</em> used in regular expressions in episode 5. Unfortunately, some small errors crept into the list of operators mentioned in that episode. These are incorrect:</p>
<ul>
<li><del>\A</del> (beginning of a string)</li>
<li><del>\z</del> (end of a string)</li>
<li><del>\b</del> (on a word boundary)</li>
</ul>
<p>The first two operators exist, in languages like Perl and Ruby, but not in GNU Awk.</p>
<p>For the ‘\b’ sequence the GNU manual says:</p>
<blockquote>
<p><em>In other GNU software, the word-boundary operator is ‘\b’. However, that conflicts with the awk language’s definition of ‘\b’ as backspace, so gawk uses a different letter. An alternative method would have been to require two backslashes in the GNU operators, but this was deemed too confusing. The current method of using ‘\y’ for the GNU ‘\b’ appears to be the lesser of two evils.</em></p>
</blockquote>
<p>The corrected list of operators is discussed later in this episode.</p>
<h3 id="replacement">Replacement</h3>
<p>Last episode we saw the built-in functions that use regular expressions for manipulating strings. These are <code>sub</code>, <code>gsub</code> and <code>gensub</code>. Regular expressions are used in other functions but we will look at them later.</p>
<p>We will be looking at <code>sub</code>, <code>gsub</code> and <code>gensub</code> in more detail in this episode.</p>
<h2 id="long-notes">Long notes</h2>
<p>I have written out a set of longer notes for this episode and these are available <a href="https://hackerpublicradio.org/eps/hpr2238/full_shownotes.html">here</a>.</p>
<h2 id="links">Links</h2>
<ul>
<li><a href="https://www.gnu.org/software/gawk/manual/html_node/index.html"><em>GNU Awk User’s Guide</em></a></li>
<li>Previous shows in this series on HPR:
<ul>
<li><a href="https://hackerpublicradio.org/eps.php?id=2114">“<em>Gnu Awk - Part 1</em>”</a> - episode 2114</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2129">“<em>Gnu Awk - Part 2</em>”</a> - episode 2129</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2143">“<em>Gnu Awk - Part 3</em>”</a> - episode 2143</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2163">“<em>Gnu Awk - Part 4</em>”</a> - episode 2163</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2184">“<em>Gnu Awk - Part 5</em>”</a> - episode 2184</li>
</ul></li>
<li>The “<em>Learning sed</em>” series:
<ul>
<li><a href="https://hackerpublicradio.org/eps.php?id=1976">“Introduction to sed - part 1”</a> - episode 1976</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=1986">“Introduction to sed - part 2”</a> - episode 1986</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=1997">“Introduction to sed - part 3”</a> - episode 1997</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2011">“Introduction to sed - part 4”</a> - episode 2011</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2060">“Introduction to sed - part 5”</a> - episode 2060</li>
</ul></li>
<li>The “<a href="https://www.mockaroo.com/">Mockaroo</a>” data generator site</li>
<li>The Vim plugin “<a href="https://github.com/chrisbra/csv.vim">csv.vim</a>”</li>
<li>Resources:
<ul>
<li><a href="https://hackerpublicradio.org/eps/hpr2238_full_shownotes.epub">ePub version of these notes</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr2238_full_shownotes.pdf">PDF version of these notes</a></li>
<li>Demonstration of some regex operators: <a href="https://hackerpublicradio.org/eps/hpr2238/contacts.awk">contacts.awk</a></li>
<li>File of dummy contacts: <a href="https://hackerpublicradio.org/eps/hpr2238/contacts.txt">contacts.txt</a></li>
</ul></li>
</ul>