<h1 id="gnu-awk---part-4">Gnu Awk - Part 4</h1>
<h2 id="introduction">Introduction</h2>
<p>This is the fourth episode of the series that <a href="https://hackerpublicradio.org/correspondents/0300.html" title="b-yeezi">b-yeezi</a> and I are doing. These shows are now collected under the series title “<a href="https://hackerpublicradio.org/series/0094.html" title="Learning Awk">Learning Awk</a>”.</p>
<h2 id="recap-of-the-last-episode">Recap of the last episode</h2>
<h3 id="logical-operators">Logical Operators</h3>
<p>We have seen the operators ‘<b>&amp;&amp;</b>’ (<em>and</em>) and ‘<b>||</b>’ (<em>or</em>). These are also called <em>Boolean Operators</em>. There is also one more operator ‘<b>!</b>’ (<em>not</em>) which we haven’t yet encountered. These operators allow the construction of <em>Boolean expressions</em> which may be quite complex.</p>
<p>If you are used to programming you will expect these operators to have a precedence, just like operators in arithmetic do. We will deal with this subject in more detail later since it is relevant not only in patterns but also in other parts of an Awk program.</p>
<h3 id="the-next-statement">The <em>next</em> statement</h3>
<p>We saw this statement in the last episode and learned that it causes the processing of the current input record to stop. No more patterns are tested against this record and no more actions in the current rule are executed. Note that “<em>next</em>” is a statement like “<em>print</em>”, and can only occur in the action part of a rule. It is also not permitted in <em>BEGIN</em> or <em>END</em> rules (more of which anon).</p>
<h3 id="the-begin-and-end-rules">The <em>BEGIN</em> and <em>END</em> rules</h3>
<p>The <em>BEGIN</em> and <em>END</em> elements are special <em>patterns</em>, which in conjunction with <em>actions</em> enclosed in curly brackets make up <em>rules</em> in the same sense that the ‘<em>pattern {action}</em>’ sequences we have seen so far are rules. As we saw in the last episode, <em>BEGIN</em> rules are run before the main ‘<em>pattern {action}</em>’ rules are processed and the input file is (or files are) read, whereas <em>END</em> rules run after the input files have been processed.</p>
<p>It is permitted to write more than one <em>BEGIN</em> rule and more than one <em>END</em> rule. These are just concatenated together in the order they are encountered by Awk.</p>
<p>Awk will complain if either <em>BEGIN</em> or <em>END</em> is not followed by an <em>action</em> since this is meaningless.</p>
<h3 id="variables-arrays-loops-etc">Variables, arrays, loops, etc</h3>
<p>Learning a programming language is never a linear process, and sometimes reference is made to new features that have not yet been explained. A number of new features were mentioned in passing in the last episode, and we will look at these in more detail in this episode.</p>
<h2 id="long-notes">Long notes</h2>
<p>I have written out a moderately long set of notes for this episode and these are available here <a href="https://hackerpublicradio.org/eps/hpr2163/full_shownotes.html" class="uri">https://hackerpublicradio.org/eps/hpr2163/full_shownotes.html</a>.</p>
<p>With a view to making portable notes for this series I have included ePub and PDF versions with this episode. Feedback is welcome to help decide which version is preferable, as are any suggestions on the improvement of the layout.</p>
<h2 id="links">Links</h2>
<ul>
<li><em>GNU Awk User’s Guide</em>: <a href="https://www.gnu.org/software/gawk/manual/html_node/index.html" class="uri">https://www.gnu.org/software/gawk/manual/html_node/index.html</a></li>
<li>Previous shows on HPR:
<ul>
<li>“<em>Gnu Awk - Part 1</em>”: <a href="https://hackerpublicradio.org/eps.php?id=2114" class="uri">https://hackerpublicradio.org/eps.php?id=2114</a></li>
<li>“<em>Gnu Awk - Part 2</em>”: <a href="https://hackerpublicradio.org/eps.php?id=2129" class="uri">https://hackerpublicradio.org/eps.php?id=2129</a></li>
<li>“<em>Gnu Awk - Part 3</em>”: <a href="https://hackerpublicradio.org/eps.php?id=2143" class="uri">https://hackerpublicradio.org/eps.php?id=2143</a></li>
</ul></li>
<li>Arithmetic expansion in Bash “<em>Some additional Bash Tips</em>”: <a href="https://hackerpublicradio.org/eps.php?id=1951" class="uri">https://hackerpublicradio.org/eps.php?id=1951</a></li>
<li>Resources:
<ul>
<li>ePub version of notes: <a href="https://hackerpublicradio.org/eps/hpr2163_full_shownotes.epub" class="uri">https://hackerpublicradio.org/eps/hpr2163_full_shownotes.epub</a></li>
<li>PDF version of notes: <a href="https://hackerpublicradio.org/eps/hpr2163_full_shownotes.pdf" class="uri">https://hackerpublicradio.org/eps/hpr2163_full_shownotes.pdf</a></li>
<li>Demonstration of arithmetic assignment operators: <a href="https://hackerpublicradio.org/eps/hpr2163/arithmetic_assignment_operators.awk" class="uri">https://hackerpublicradio.org/eps/hpr2163/arithmetic_assignment_operators.awk</a></li>
<li>Counting frequencies of particular colours: <a href="https://hackerpublicradio.org/eps/hpr2163/color_count.awk" class="uri">https://hackerpublicradio.org/eps/hpr2163/color_count.awk</a></li>
</ul></li>
</ul>