<h1 id="awk-part-3">Awk Part 3</h1>
<p>Remember our file:</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>
<h2 id="replace-grep">Replace Grep</h2>
<p>As we saw in earlier episodes, we can use awk to filter for rows that match a pattern or text. If you know the grep command, you know that it does the same function, but has extended capabilities. For simple filter, you don't need to pipe grep outputs to awk. You can just filter in awk.</p>
<h2 id="logical-operators">Logical Operators</h2>
<p>You can use logical operators &quot;and&quot; and &quot;or&quot; represented as &quot;&amp;&amp;&quot; and &quot;||&quot;, respectively. See example:</p>
<pre><code>$2 == &quot;purple&quot; &amp;&amp; $3 &lt; 5 {print $1}</code></pre>
<p>Here, we are selecting for color to to equal &quot;purple&quot; AND amount less than 5.</p>
<h2 id="next-command">Next command</h2>
<p>Say we want to flag every record in our file where the amount is greater than or equal to 8 with a '**'. Every record between 5 (inclusive) and 8, we want to flag with a '*'. We can use consecutive filter commands, but there affects will be additive. To remedy this, we can use the &quot;next&quot; command. This tells awk that after the action is taken, proceed to the next record. See the following example:</p>
<pre><code>NR == 1 {
  print $0;
  next;
}

$3 &gt;= 8 {
  printf &quot;%s\t%s\n&quot;, $0, &quot;**&quot;;
  next;
}

$3 &gt;= 5 {
  printf &quot;%s\t%s\n&quot;, $0, &quot;*&quot;;
  next;
}

$3 &lt; 5 {
  print $0;
}</code></pre>
<h2 id="end-command">End Command</h2>
<p>The &quot;BEGIN&quot; and &quot;END&quot; commands allow you to do actions before and after awk does its actions. For instance, sometimes we want to evaluate all records, then print the cumulative results. In this example, we pipe the output of the df command into awk. Our command is:</p>
<pre><code>df -l | awk -f end.awk</code></pre>
<p>Our awk file looks like this:</p>
<pre><code>$1 != &quot;tmpfs&quot; {
    used += $3;
    available += $4;
}

END {
    printf &quot;%d GiB used\n%d GiB available\n&quot;, used/2^20, available/2^20;
}</code></pre>
<p>Here, we are setting two variables, &quot;used&quot; and &quot;available&quot;. We add the records in the respective columns all together, then we print the totals.</p>
<p>In the next example, we create a distinct list of colors from our file:</p>
<pre><code>NR != 1 {
    a[$2]++
}
END {
    for (b in a) {
        print b
    }
}</code></pre>
<p>This is a more advanced script. The details of which, we will get into in future episodes.</p>
<h2 id="begin-command">BEGIN command</h2>
<p>Like stated above, the begin command lets us print and set variables before the awk command starts. For instance, we can set the input and output field separators inside our awk file as follows:</p>
<pre><code>BEGIN {
    FS=&quot;,&quot;;
    OFS=&quot;,&quot;;
    print &quot;color,count&quot;;
}
NR != 1 {
    a[$2]+=1;
}
END {
    for (b in a) {
        print b, a[b]
    }
}</code></pre>
<p>In this example, we are finding the distinct count of colors in our csv file, and format the output in csv format as well. We will get into the details of how this script works in future episodes.</p>
<p>For another example, instead of distinct count, we can get the sum of the amount column grouped by color:</p>
<pre><code>BEGIN {
    FS=&quot;,&quot;;
    OFS=&quot;,&quot;;
    print &quot;color,sum&quot;;
}
NR != 1 {
    a[$2]+=$3;
}
END {
    for (b in a) {
        print b, a[b]
    }
}</code></pre>
