Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement tail with awk

I am struggling with this awk code which should emulate the tail command

num=$1;
{
    vect[NR]=$0;
    
}
END{
    for(i=NR-num;i<=NR;i++)
            print vect[$i]
}

So what I'm trying to achieve here is an tail command emulated by awk/ For example consider cat somefile | awk -f tail.awk 10 should print the last 10 lines of a text file, any suggestions?

like image 559
Lucian Enache Avatar asked Dec 03 '22 05:12

Lucian Enache


1 Answers

All of these answers store the entire source file. That's a horrible idea and will break on larger files.

Here's a quick way to store only the number of lines to be outputted (note that the more efficient tail will always be faster because it doesn't read the entire source file!):

awk -vt=10 '{o[NR%t]=$0}END{i=(NR<t?0:NR);do print o[++i%t];while(i%t!=NR%t)}'

more legibly (and with less code golf):

awk -v tail=10 '
  {
    output[NR % tail] = $0
  }
  END {
    if(NR < tail) {
      i = 0
    } else {
      i = NR
    }
    do {
      i = (i + 1) % tail;
      print output[i]
    } while (i != NR % tail)
  }'

Explanation of legible code:

This uses the modulo operator to store only the desired number of items (the tail variable). As each line is parsed, it is stored on top of older array values (so line 11 gets stored in output[1]).

The END stanza sets an increment variable i to either zero (if we've got fewer than the desired number of lines) or else the number of lines, which tells us where to start recalling the saved lines. Then we print the saved lines in order. The loop ends when we've returned to that first value (after we've printed it).

You can replace the if/else stanza (or the ternary clause in my golfed example) with just i = NR if you don't care about getting blank lines to fill the requested number (echo "foo" |awk -vt=10 … would have nine blank lines before the line with "foo").

like image 107
Adam Katz Avatar answered Dec 13 '22 08:12

Adam Katz