Linux – Using “tail” to follow a file without displaying the most recent lines

command linelinuxtail

I would like use a program like tail to follow a file as it's being written to, but not display the most recent lines.

For instance, when following a new file, no text will be displayed while the file is less than 30 lines. After more than 30 lines are written to the file, lines will be written to the screen starting at line 1.

So as lines 31-40 are written to the file, lines 1-10 will be written to the screen.

If there is no easy way to do this with tail, maybe a there's a way to write to a new file a prior line from the first file each time the first file is extended by a line, and the tail that new file…

Best Answer

Maybe buffer with awk:

tail -n +0 -f some/file | awk '{b[NR] = $0} NR > 30 {print b[NR-30]; delete b[NR-30]} END {for (i = NR - 29; i <= NR; i++) print b[i]}'

The awk code, expanded:

{
    b[NR] = $0 # save the current line in a buffer array
}
NR > 30 { # once we have more than 30 lines
    print b[NR-30]; # print the line from 30 lines ago
    delete b[NR-30]; # and delete it
}
END { # once the pipe closes, print the rest
    for (i = NR - 29; i <= NR; i++)
        print b[i]
}
Related Question