Implement tail with awk

后端 未结 4 1555
失恋的感觉
失恋的感觉 2021-01-19 16:44

Alright so here i am struggling with this awk code wich should emulate the tail command

num=$1;
{
    vect[NR]=$0;

}
END{
    for(i=NR-num;i<=NR;i++)
            


        
4条回答
  •  无人共我
    2021-01-19 17:35

    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

    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").

提交回复
热议问题