<h1 id="awk-series-part-9---printf">Awk Series Part 9 - printf</h1>
<p>The <code>printf</code> function allows for greater control over the output, in comparison to <code>print</code>.</p>
<p>To follow along, you can either use these show notes or refer to <a href="https://www.gnu.org/software/gawk/manual/gawk.html">the gawk manual</a>.</p>
<p>There are 3 main areas to cover:</p>
<ul>
<li>Basic <code>printf</code> syntax</li>
<li>Format Control letters</li>
<li>Format modifiers</li>
</ul>
<h2 id="syntax">Syntax</h2>
<pre><code>printf format, item1, item2, …</code></pre>
<p>The big difference in the syntax of printf statements is the <em>format</em> argument. It allows you to use complex formatting and layouts for outputs. Unlike <code>print</code>, <code>printf</code> does not automatically start a new line after the function. This can be useful when you want to print all of the items in a column on a single line.</p>
<p>For example, remember the example file, file1.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>Look at the difference between the following outputs:</p>
<pre><code>awk -F, &#39;NR!=1{print &quot;Color&quot;, $2, &quot;has&quot;, $3}&#39; file1.csv</code></pre>
<p>and</p>
<pre><code>awk -F, &#39;NR!=1{printf &quot;Color %s has %s. &quot;, $2, $3}&#39; file1.csv</code></pre>
<h2 id="control-letters">Control Letters</h2>
<p>Control letters control or cast the output to specific types. Use it as a way to convert ints to floats, ints to chars, etc.</p>
<p><code>%c</code> = to char. <code>printf &quot;%c&quot;, 65</code> prints a<br />
<code>%i</code>, <code>%d</code> = to int. <code>printf &quot;%i&quot;, 3.4</code> prints 3<br />
<code>%f</code> = to float. <code>printf &quot;%c&quot;, 65</code> prints 65.000000<br />
<code>%e</code>, <code>%E</code> = to scientific notation. <code>printf &quot;%e&quot;, 65</code> prints 6.500000e+01. If you use <code>%E</code> will use a capital E instead of e.<br />
<code>%g</code> = to either scientific notation or int. <code>printf &quot;%.2g&quot;, 65</code> prints 65, while <code>printf &quot;%.1g&quot;, 65</code> prints 6e+01<br />
<code>%s</code> = to string. <code>printf &quot;%s&quot;, 65</code> prints 65<br />
<code>%u</code> = to unsigned int. <code>printf &quot;%u&quot;, -6</code> prints 18446744073709551610</p>
<p>There are others. See documentation.</p>
<h2 id="formatting">Formatting</h2>
<p><code>N$</code> = positional specifier. <code>printf &quot;%2$s %1$s&quot;, &quot;second&quot;, &quot;first&quot;</code><br />
<code>n</code> = spaces to the left of the string.<br />
<code>-n</code> = spaces to the right of string.<br />
<code>space</code> = prefix positive numbers with a space, negative numbers with a -<br />
<code>+</code> = prefix all numbers with a sign (either + or -)<br />
<code>0n</code> = leading 0's before input. <code>printf &quot;%03i&quot;, 65</code> prints 065.<br />
<code>'</code> = comma place holder for thousands. <code>printf &quot;%'i&quot;, 6500</code> prints 6,500</p>
<p>Below is an (crude) illustration of how I like to think when formatting output:</p>
<pre><code>          7          2
├──────┼───────┼────┼──┤
 Color: RedXXXX Sum: X6</code></pre>
<pre><code>       18            3
├──────────────────╂───┤
 Total Sum:XXXXXXXX X34</code></pre>
<p>See the following awk file</p>
<pre><code>BEGIN {
    FS=&quot;,&quot;;
}
NR != 1 {
    a[$2]+=$3;
    c+=$3;
    d+=1;
}
END {
    for (b in a) {
        printf &quot;Color: %-7s Sum: %2i\n&quot;, b, a[b];
    }
    print &quot;----------------------&quot;
    printf &quot;%-18s %3i\n&quot;, &quot;Total Sum:&quot;, c;
    printf &quot;%-18s %3i\n&quot;, &quot;Total Count:&quot;, d;
    printf &quot;%-18s %3.1f\n&quot;, &quot;Mean:&quot;, c / d;
}</code></pre>
<p>This gives the following output:</p>
<pre><code>Color: brown   Sum: 13
Color: purple  Sum: 12
Color: red     Sum:  7
Color: yellow  Sum: 11
Color: green   Sum:  8
----------------------
Total Sum:          51
Total Count:         9
Mean:              5.7</code></pre>
<h1 id="resources">Resources</h1>
<ol type="1">
<li><a href="https://www.gnu.org/software/gawk/manual/gawk.html#Printf" class="uri">https://www.gnu.org/software/gawk/manual/gawk.html#Printf</a></li>
<li><a href="https://www.grymoire.com/Unix/Awk.html" class="uri">https://www.grymoire.com/Unix/Awk.html</a></li>
<li><a href="https://datascienceatthecommandline.com/" class="uri">https://datascienceatthecommandline.com/</a></li>
</ol>
