Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try the following code:</p> <pre class="lang-sh prettyprint-override"><code>sar -u|head -5|awk ' BEGIN { hname="'"`hostname -s`"'"; hdt="'"`date +"%d-%b-%Y" `"'" ; OFS="," ;} $1 ~ /[0-9]/ &amp;&amp; $4 ~ /[0-9]/ { printf( "h=%-15s d=%11s 1=%8s 2=%2s 3=%-3s 4=%6.2f 5=%6.2f 6=%6.2f 7=%6.2f 8=%6.2f 9=%6.2f\n", hname, hdt, $1, $2, $3, $4, $5, $6, $7, $8, $9 );}' </code></pre> <p>Note that this is untested, since I do not have the <code>sar</code> program installed. If you paste some example output of that program, I can test the code and update my answer accordingly.</p> <p>This is as closest to your code as possible and should work if I did not miss anything else. The <code>-F</code> option you used did nothing since you did not specify a new <em>field separator</em> (<code>FS</code>). Note that I removed the braces (curly brackets, <code>{...}</code>) around your <em>"search</em>" statement <code>$1 ~ /[0-9]/ &amp;&amp; $4 ~ /[0-9]/</code>.</p> <p>An <code>awk</code> program consists of <em>patterns</em> and associated <em>actions</em>, that are performed for those <em>records</em> (without further options, each line is considered one <em>record</em>) matching the <em>pattern</em>. The <em>action</em> has to be enclosed in braces after the corresponding <em>pattern</em>.</p> <p>So your code has three <em>actions</em>: The initialization that is associated to the special <em>BEGIN pattern</em> and thus performed once before reading any input and <strong>two</strong> actions that are performed for all <em>records</em> (i.e. lines) since they are not associated to any <em>pattern</em>: The first evaluates your search expression and the second prints the formatted <em>record</em>. Note, however, that these two <em>actions</em> are <strong>not</strong> linked and the print action is performed no matter whether your search expression evaluated to true or false.</p> <p>To understand what was wrong with your code you can run this verbose version:</p> <pre class="lang-sh prettyprint-override"><code>sar -u|head -5|awk ' BEGIN { hname="'"`hostname -s`"'"; hdt="'"`date +"%d-%b-%Y" `"'" ; OFS="," ;} {print "search returned "($1 ~ /[0-9]/ &amp;&amp; $4 ~ /[0-9]/?"true.":"false.")} { printf( "h=%-15s d=%11s 1=%8s 2=%2s 3=%-3s 4=%6.2f 5=%6.2f 6=%6.2f 7=%6.2f 8=%6.2f 9=%6.2f\n", hname, hdt, $1, $2, $3, $4, $5, $6, $7, $8, $9 );}' </code></pre> <p>This expands your first <em>action</em> to print the result of your search expression and you will see that the second (i.e. the <code>printf</code>) <em>action</em> is performed independently of that result.</p> <p>By removing the braces around your search expression, you make that a <em>pattern</em> associated with the next <em>action</em>, the <code>printf</code> one. This way the <code>printf</code> is performed only for those <em>records</em> (i.e. lines) that match your search <em>pattern</em>.</p> <p>Further note that your code gets more readable if you store the external commands' output in variables <a href="https://stackoverflow.com/a/17276728/2451238">as suggested</a> by <a href="https://stackoverflow.com/users/970195/jaypal-singh">@jaypal singh</a>, in the following achieved by initializing <code>awk</code> variables using the <code>-v</code> option:</p> <pre class="lang-sh prettyprint-override"><code>sar -u|head -5|awk \ -v hname="`hostname -s`" \ -v hdt="`date +"%d-%b-%Y"`" \ -v OFS="," \ ' $1 ~ /[0-9]/ &amp;&amp; $4 ~ /[0-9]/{ printf( "h=%-15s d=%11s 1=%8s 2=%2s 3=%-3s 4=%6.2f 5=%6.2f 6=%6.2f 7=%6.2f 8=%6.2f 9=%6.2f\n", hname, hdt, $1, $2, $3, $4, $5, $6, $7, $8, $9 ) } ' </code></pre> <p><strong>Addendum:</strong></p> <p><a href="https://stackoverflow.com/a/17276728/2451238">As pointed out</a> by <a href="https://stackoverflow.com/users/970195/jaypal-singh">@jaypal singh</a>, the <em>output field separator</em> (<code>OFS</code>) will not affect <code>prinft</code> so you might want to use comma (<code>,</code>) instead of space (<code></code>) in your <code>printf</code> statement instead of defining <code>OFS</code>:</p> <pre class="lang-sh prettyprint-override"><code>sar -u|awk \ -v hname="`hostname -s`" \ -v hdt="`date +"%d-%b-%Y"`" \ ' $1 ~ /[0-9]/ &amp;&amp; $4 ~ /[0-9]/{ printf( "h=%-15s,d=%11s,1=%8s,2=%2s,3=%-3s,4=%6.2f,5=%6.2f,6=%6.2f,7=%6.2f,8=%6.2f,9=%6.2f\n", hname, hdt, $1, $2, $3, $4, $5, $6, $7, $8, $9 ) } ' </code></pre> <p>Note that this time I also removed <code>head</code> from the pipeline since you stated in a comment that this was just introduced for testing reasons and if it was not using the <em>record number</em> (<code>NR</code>) would have been better <a href="https://stackoverflow.com/a/17276728/2451238">as suggested</a> by <a href="https://stackoverflow.com/users/970195/jaypal-singh">@jaypal singh</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload