<h2 id="gnu-awk---part-5">GNU AWK - Part 5</h2>
<h3 id="regular-expressions-in-awk">Regular Expressions in AWK</h3>
<p>The syntax for using regular expressions to match lines in AWK is as follows:</p>
<pre><code>word ~ /match/</code></pre>
<p>Or for not matching, use the following:</p>
<pre><code>word !~ /match/</code></pre>
<p>Remember the following file from the previous episodes:</p>
<pre><code>name       color  amount
apple      red    4
banana     yellow 6
strawberry red    3
grape      purple 10
apple      green  8
plum       purple 2
kiwi       brown  4
potato     brown  9
pineapple  yellow 5</code></pre>
<p>We can run the following command:</p>
<pre><code>$1 ~ /p[elu]/ {print $0}</code></pre>
<p>We will get the following output:</p>
<pre><code>apple      red    4
grape      purple 10
apple      green  8
plum       purple 2
pineapple  yellow 5</code></pre>
<p>In another example:</p>
<pre><code>$2 ~ /e{2}/ {print $0}</code></pre>
<p>Will produce the output:</p>
<pre><code>apple      green  8</code></pre>
<h3 id="regular-expression-basics">Regular expression basics</h3>
<p>Certain characters have special meaning when using regular expressions.</p>
<h4 id="anchors">Anchors</h4>
<ul>
<li><code>^</code> - beginning of the line</li>
<li><code>$</code> - end of the line</li>
<li><code>\A</code> - beginning of a string</li>
<li><code>\z</code> - end of a string</li>
<li><code>\b</code> on a word boundary</li>
</ul>
<h4 id="characters">Characters</h4>
<ul>
<li><code>[ad]</code> - a or d</li>
<li><code>[a-d]</code> - any character a through d</li>
<li><code>[^a-d]</code> - not any character a through d</li>
<li><code>\w</code> - any word</li>
<li><code>\s</code> - any white-space character</li>
<li><code>\d</code> - any digit</li>
</ul>
<p>The capital version of w, s, and d are negations.</p>
<p>Or, you can reference characters the POSIX standard way:</p>
<ul>
<li><code>[:alnum:]</code> - Alphanumeric characters</li>
<li><code>[:alpha:]</code> - Alphabetic characters</li>
<li><code>[:blank:]</code> - Space and TAB characters</li>
<li><code>[:cntrl:]</code> - Control characters</li>
<li><code>[:digit:]</code> - Numeric characters</li>
<li><code>[:graph:]</code> - Characters that are both printable and visible (a space is printable but not visible, whereas an ‘a’ is both)</li>
<li><code>[:lower:]</code> - Lowercase alphabetic characters</li>
<li><code>[:print:]</code> - Printable characters (characters that are not control characters)</li>
<li><code>[:punct:]</code> - Punctuation characters (characters that are not letters, digits, control characters, or space characters)</li>
<li><code>[:space:]</code> - Space characters (such as space, TAB, and formfeed, to name a few)</li>
<li><code>[:upper:]</code> - Uppercase alphabetic characters</li>
<li><code>[:xdigit:]</code> - Characters that are hexadecimal digits</li>
</ul>
<h4 id="quantifiers">Quantifiers</h4>
<ul>
<li><code>.</code> - match any character</li>
<li><code>+</code> - match preceding one or more times</li>
<li><code>*</code> - match preceding zero or more times</li>
<li><code>?</code> - match preceding zero or one time</li>
<li><code>{n}</code> - match preceding exactly n times</li>
<li><code>{n,}</code> - match preceding n or more times</li>
<li><code>{n,m}</code> - match preceding between n and m times</li>
</ul>
<h4 id="grouped-matches">Grouped Matches</h4>
<ul>
<li><code>(...)</code> - Parentheses are used for grouping</li>
<li><code>|</code> - Means <em>or</em> in the context of a grouped match</li>
</ul>
<h3 id="replacement">Replacement</h3>
<ul>
<li>The <code>sub</code> command substitutes the match with the replacement string. This only applies to the first match.</li>
<li>The <code>gsub</code> command substitutes all matching items.</li>
<li>The <code>gensub</code> command command substitutes the in a similar way as sub and gsub, but with extra functionality</li>
<li>The <code>&amp;</code> character in the replacement field references the matched text. You have to use <code>\&amp;</code> to replace the match with the literal <em>&amp;</em> character.</li>
</ul>
<p>Example:</p>
<pre><code>{ sub(/apple/, &quot;nut&quot;, $1);
    print $1}</code></pre>
<p>The output is:</p>
<pre><code>name
nut
banana
strawberry
grape
nut
plum
kiwi
potato
pinenut</code></pre>
<p>Another example:</p>
<pre><code>{ sub(/.+(pp|rr)/, &quot;test-&amp;&quot;, $1);
    print $1}</code></pre>
<p>This produces the following output:</p>
<pre><code>name
test-apple
banana
test-strawberry
grape
test-apple
plum
kiwi
potato
test-pineapple</code></pre>
<h2 id="resources">Resources</h2>
<ul>
<li><a href="https://regex101.com">Regex 101</a> - Advanced online regex tester</li>
<li><a href="https://regexr.com">RegExr</a> - Simple online regex tester</li>
<li><a href="https://www.grymoire.com/Unix/Awk.html">Grymoire's Awk Tutorial</a> - Great Resource for understanding Awk</li>
<li><a href="https://www.gnu.org/software/gawk/manual/html_node/Regexp.html">GNU Awk's Regex User Guide</a> - Official Gawk user guide</li>
</ul>
