<h1 id="introduction-to-awk">Introduction to Awk</h1>
<p>Awk is a powerful text parsing tool for unix and unix-like systems.</p>
<p>The basic syntax is:</p>
<pre><code>awk [options] &#39;pattern {action}&#39; file</code></pre>
<p>Here is a simple example file that we will be using, called <code>file1.txt</code>:</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>First command:</p>
<pre><code>awk &#39;{print $2}&#39; file1.txt</code></pre>
<p>As you can see, the “print” command will display the whatever follows. In this case we are showing the second column using “$2”. This is intuitive. To display all columns, use “$0”.</p>
<p>This example will output:</p>
<pre><code>color
red
yellow
red
purple
green
purple
brown
brown
yellow</code></pre>
<p>Second command:</p>
<pre><code>awk &#39;$2==&quot;yellow&quot;{print $1}&#39; file1.txt</code></pre>
<p>This will output:</p>
<pre><code>banana
pineapple</code></pre>
<p>As you can see, the command matches items in column 2 matching “yellow”, but prints column 1.</p>
<h2 id="field-separator">Field separator</h2>
<p>By default, awk uses white space as the file separator. You can change this by using the -F option. For instance, <code>file1.csv</code> looks like this:</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>A similar command as before:</p>
<pre><code>awk -F&quot;,&quot; &#39;$2==&quot;yellow&quot; {print $1}&#39; file1.csv</code></pre>
<p>will still output:</p>
<pre><code>banana
pineapple</code></pre>
<p>Regular expressions work as well:</p>
<pre><code>awk &#39;$2 ~ /p.+p/ {print $0}&#39; file1.txt</code></pre>
<p>This returns:</p>
<pre><code>grape   purple  10
plum    purple  2</code></pre>
<p>Numbers are interpreted automatically:</p>
<pre><code>awk &#39;$3&gt;5 {print $1, $2}&#39; file1.txt</code></pre>
<p>Will output:</p>
<pre><code>name    color
banana  yellow
grape   purple
apple   green
potato  brown</code></pre>
<p>Using output redirection, you can write your results to file. For example:</p>
<pre><code>awk -F, &#39;$3&gt;5 {print $1, $2}&#39; file1.csv &gt; output.txt</code></pre>
<p>This will output a file with the contents of the query.</p>
<p>Here’s a cool trick! You can automatically split a file into multiple files grouped by column. For example, if I want to split file1.txt into multiple files by color, here is the command.</p>
<pre><code>awk &#39;{print &gt; $2&quot;.txt&quot;}&#39; file1.txt</code></pre>
<p>This will produce files named yellow.txt, red.txt, etc. In upcoming episodes, we will show how to improve the outputs.</p>
<h1 id="resources">Resources</h1>
<ol type="1">
<li><a href="https://www.theunixschool.com/p/awk-sed.html" class="uri">https://www.theunixschool.com/p/awk-sed.html</a></li>
<li><a href="https://www.tecmint.com/category/awk-command/" class="uri">https://www.tecmint.com/category/awk-command/</a></li>
<li><a href="https://linux.die.net/man/1/awk" class="uri">https://linux.die.net/man/1/awk</a></li>
</ol>
<h1 id="coming-up">Coming up</h1>
<ul>
<li>More options</li>
<li>Built-in Variables</li>
<li>Arithmetic operations</li>
<li>Awk language and syntax</li>
</ul>
