<p>In this episode, I will (very) briefly go over loops in the Awk programming language. Loops are useful when you want to run the same command(s) on a collection of data or when you just want to repeat the same commands many times.</p>
<p>When using loops, a command or group of commands is repeated until a condition (or many) is met.</p>
<h2 id="while-loop">While Loop</h2>
<p>Here is a silly example of a while loop:</p>
<pre><code>#!/bin/awk -f
BEGIN {

# Print the squares from 1 to 10 the first way

    i=1;
    while (i &lt;= 10) {
        print &quot;The square of &quot;, i, &quot; is &quot;, i*i;
        i = i+1;
    }

exit;
}</code></pre>
<p>Our condition is set in the braces after the <code>while</code> statement. We set a variable, i, before entering the loop, then increment i inside of the loop. If you forget to make a way to meet the condition, the while will go on <strong>forever</strong>.</p>
<h2 id="do-while-loop">Do While Loop</h2>
<p>Here is an equally silly example of a <code>do while</code> loop:</p>
<pre><code>#!/bin/awk -f
BEGIN {

    i=2;
    do {
        print &quot;The square of &quot;, i, &quot; is &quot;, i*i;
        i = i + 1
    }

    while (i != 2)

exit;
}</code></pre>
<p>Here, the commands in the <code>do</code> code block are executed at the start, then the looping begins.</p>
<h2 id="for-loop">For Loop</h2>
<p>Another silly example of a <code>for</code> loop:</p>
<pre><code>#!/bin/awk -f
BEGIN {

    for (i=1; i &lt;= 10; i++) {
        print &quot;The square of &quot;, i, &quot; is &quot;, i*i;
    }

exit;
}</code></pre>
<p>As you can see, we set the variable, set the condition and set the increment method all in the braces after the <code>for</code> statement.</p>
<h2 id="for-loop-over-arrays">For Loop Over Arrays</h2>
<p>Here is a more useful example of a for loop. Here, we are adding the different values of column 2 into an array/hash-table called <code>a</code>. After processing the file, we print the different values.</p>
<p>For file.txt:</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>Using the awk file of:</p>
<pre><code>NR != 1 {
    a[$2]++
}
END {
    for (b in a) {
        print b
    }
}</code></pre>
<p>We get the results of:</p>
<pre><code>brown
purple
red
yellow
green</code></pre>
<p>In another example, we do a similar process. This time, not only do we store all the distinct values of the second column, we perform a sum operation on column 3 for each distinct value of column 2.</p>
<p>For file.csv:</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>Using the awk file of:</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>
<p>We get the results of:</p>
<pre><code>color,sum
brown,13
purple,12
red,7
yellow,11
green,8</code></pre>
<p>As you can see, we are also printing a header column prior to processing the file using the <code>BEGIN</code> code block.</p>
