Using "tail" to follow a file without displaying the most recent lines
Same as @muru's but using the modulo operator instead of storing and deleting:
tail -fn+1 some/file | awk -v n=30 '
NR > n {print s[NR % n]}
{s[NR % n] = $0}
END{for (i = NR - n + 1; i <= NR; i++) print s[i % n]}'
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]
}
This isn't very efficient, because it will re-read the file two seconds after reading it last time, and you will miss lines if the output is coming too fast, but will otherwise do the job:
watch 'tail -n40 /path/to/file | head -n10'