简体   繁体   English

如何拖尾-F,但仅在最后5行滚动显示

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

I'd like to be able to tail -F some output, but not have it scrollback the entire buffer, only scroll inside a limited number of lines, say 5 lines. 我希望能够在tail -F一些输出,但是不希望它回滚整个缓冲区,而仅在有限的几行(例如5行)内滚动。

How can I do this? 我怎样才能做到这一点?

I tried 我试过了

tail -F -n 5 /tmp/dump

but that doesn't seem to work - scrolling lines take up the whole buffer 但这似乎不起作用-滚动线占用了整个缓冲区

The following solution isn't pretty - it uses an ANSI escape sequence - but I think it does roughly what you want without using watch : 以下解决方案不是很漂亮-它使用ANSI转义序列-但我认为它无需使用watch就可以大致满足您的要求:

while true; do
    tail -5 /tmp/dump | cut -c1-80
    printf '\e[5A'
    sleep 1
done

The sequence \\e[5A means go up five lines. 序列\\e[5A表示上升五行。 The 5 can be replaced with whatever number you'd like. 5可以替换为您想要的任何数字。

That said, you'd be better off using a curses-like library for this kind of thing. 也就是说,最好使用类似curses的库来处理此类事情。 Using raw ANSI escape sequences isn't portable. 使用原始ANSI转义序列是不可移植的。 tput is avaiable in Linux and Cygwin. tput在Linux和Cygwin中可用。 The cuu capability moves up lines. cuu能力向上移动。

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

tail -5f /tmp/dump Weirdly this is different across distros. tail -5f /tmp/dump奇怪的是,这在发行版中是不同的。 For some reason the --retry doesn't work with it. 由于某些原因,-- --retry无法使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM