one-liner: print all lines except the last 3?

后端 未结 10 1212
灰色年华
灰色年华 2021-01-18 01:46

I would like to simulate GNU\'s head -n -3, which prints all lines except the last 3, because head on FreeBSD doesn\'t have this feature. So I am t

10条回答
  •  暖寄归人
    2021-01-18 01:53

    Here's a late answer, because I was running into something like this yesterday.

    This solution is:

    • pure bash
    • one-liner
    • reads the input stream only once
    • reads the input stream line-by-line, not all at once

    Tested on Ubuntu, Redhat and OSX.

    $ seq 1 10 | { n=3; i=1; while IFS= read -r ln; do [ $i -gt $n ] && cat <<< "${buf[$((i%n))]}"; buf[$((i%n))]="$ln"; ((i++)); done; }
    1
    2
    3
    4
    5
    6
    7
    $ 
    

    It works by reading lines into a circular buffer implemented as an n-element array.

    n is the number of lines to cut off the end of the file.

    For every line i we read, we can echo the line i-n from the circular buffer, then store the line i in the circular buffer. Nothing is echoed until the first n lines are read. (i mod n) is the index into the array which implements the circular buffer.

    Because the requirement is for a one-liner, I tried to make it fairly brief, unfortunately at the expense of readability.

提交回复
热议问题