如何 tail -F,但只在最后 5 行保持滚动输出

How to tail -F, but keep it only scrolling output in last 5 lines

我希望能够 tail -F 一些输出,但不让它回滚整个缓冲区,只在有限的行内滚动,比如 5 行。

我该怎么做?

我试过了

tail -F -n 5 /tmp/dump

但这似乎不起作用 - 滚动线占据了整个缓冲区

tail -5f /tmp/dump 奇怪的是,这在不同的发行版中是不同的。出于某种原因,--retry 无法使用它。

下面的解决方案并不完美 - 它使用了 ANSI 转义序列 - 但我认为它在不使用 watch:

的情况下大致可以满足你的需求
while true; do
    tail -5 /tmp/dump | cut -c1-80
    printf '\e[5A'
    sleep 1
done

序列\e[5A表示向上五行。 5 可以替换为您想要的任何数字。

就是说,对于这种事情,您最好使用类似 curses 的库。使用原始 ANSI 转义序列不可移植。 tput 在 Linux 和 Cygwin 中可用。 cuu 能力向上移动。

while true; do
    tail -5 /tmp/dump | cut -c1-80
    tput cuu 5
    sleep 1
done