How to delete last two lines of an output using AWK?
<p>i am trying to make a graphic representation of an output that shows the free memory of the server of the current day (which is shown approximetaly by hour), from that output i dont want the first two lines and last two, the first two i can easy delete with the NR, though i have had trouble with the last two, the only way i found was to reverse the whole output delete the first two lines and then reverse it again to its normal state.</p>

<p>Any better/correct way to delete the last two lines but do it inside the AWK?</p>

<p>this is what i have so far and works fine.</p>

<pre><code>echo 'graphics (freemem)'
echo 'GiB      0      500k      1M      1,5M'
sar -r | tail -r | awk 'NR &gt; 2{ print }' | tail -r | nawk '
    NR &gt; 4{
    LINE = sprintf("%" int(($2 / 100000)*2 )"s", " "); 
    gsub(/ /, "#", LINE);
    print $1, LINE; } 
'
</code></pre>

<p><strong>UPDATE</strong></p>

<p>So i did it in another way because it would easier the script to read and it works for me, but for any other guy that needs to do that use either the previous way i did it, or the ways that the guys below have answered.</p>

<pre><code>echo 'graphics (freemem)'
echo 'GiB      0      500k      1M      1,5M'
sar -r | nawk '
    NR &gt; 4 &amp;&amp; $1 !~ /Average/ &amp;&amp; $1 !~ /^$/ {
    LINE = sprintf("%" int(($2 * 2) / 100000 )"s", " "); 
    gsub(/ /, "#", LINE);
    print $1, LINE; } 
'
</code></pre>

<p><strong>UPDATE 2</strong></p>

<p>So what i want to make is a graphic representation of data monitored, this output updates about each hour, keep in mind that in different hours of the day the length of the rows vary. So this the actual ouput of <code>sar -r</code> that i get.</p>

<pre><code>.
SunOS doc 5.10

00:00:00 freemem freeswap
01:00:01 1624430 19474799
02:00:00 1624421 19474738
03:00:00 1624413 19474683
04:00:00 1624527 19475309
05:00:00 1624525 19475290
06:00:00 1624517 19475232
07:00:01 1624510 19475176
08:00:00 1624503 19475120
08:20:00 1539793 18771472
08:40:00 1509924 18535201
09:00:00 1500681 18492050
09:20:00 1494254 18435535
09:40:00 1479623 18266664
10:00:00 1486372 18317241
10:20:00 1480565 18269032
10:40:00 1479030 18260247
11:00:01 1476462 18237731
11:20:00 1475999 18234221
11:40:00 1478854 18259827
12:00:00 1478223 18254861
12:20:00 1482956 18294572
12:40:00 1479061 18260565
13:00:00 1470463 18185441
13:20:00 1474090 18217961
13:40:00 1478585 18257393
14:00:00 1478219 18255445
14:20:00 1474327 18223749
14:40:00 1470468 18190676
15:00:00 1471941 18204282
15:20:00 1474138 18223380
15:40:00 1471698 18202561
16:00:00 1470316 18191099
16:20:00 1467203 18163653
16:40:00 1468943 18179148

Average  1549994 18855701
</code></pre>

<p>So we dont care about freeswap and we dont use that. We care only for the results of freemem. So what i am trying to do is take as entry that column and "translate" the number into " # " through a calculation so i can later specify the amount of data in the graphic representation. Though, there are rows in the start and in the end of the output that i dont need so i need them deleted.</p>

<p>This is the working code i have now</p>

<pre><code>echo '\ngraphics (freemem)'
echo 'GiB      0      500k      1M      1,5M'
sar -r | awk 'NR &gt; 2 {print P2} {P2=P; P=$0}' |
nawk '
    NR &gt; 4 {
    LINE = sprintf("%" int(($2 * 2) / 100000 )"s", " "); 
    gsub(/ /, "#", LINE);
    print $1, LINE; } 
'
</code></pre>

<p>so the correct output is this one:</p>

<pre><code>graphics (freemem)
GiB      0      500k      1M      1,5M
01:00:01 ################################
02:00:00 ################################
03:00:00 ################################
04:00:00 ################################
05:00:00 ################################
06:00:00 ################################
07:00:01 ################################
08:00:00 ################################
08:20:00 ##############################
08:40:00 ##############################
09:00:00 ##############################
09:20:00 #############################
09:40:00 #############################
10:00:00 #############################
10:20:00 #############################
10:40:00 #############################
11:00:01 #############################
11:20:00 #############################
11:40:00 #############################
12:00:00 #############################
12:20:00 #############################
12:40:00 #############################
13:00:00 #############################
13:20:00 #############################
13:40:00 #############################
14:00:00 #############################
14:20:00 #############################
14:40:00 #############################
15:00:00 #############################
15:20:00 #############################
15:40:00 #############################
16:00:00 #############################
16:20:00 #############################
</code></pre>