【问题标题】:clear screen when the file is truncated while using `tail -f`使用`tail -f`时文件被截断时清除屏幕
【发布时间】:2012-10-08 16:09:43
【问题描述】:

我正在使用tail -f 打印不断变化的文件的内容。当文件被截断时,它会显示如下:

blah (old)..
blah more (old)..
tail: file.out: file truncated
blah..
blah more..

如果我过于频繁地更改文件,这会变得一团糟,以至于很难看到文件的开始/结束位置。有没有办法在文件被截断时以某种方式clear 屏幕,使其显示为这样?

tail: file.out: file truncated
blah..
blah more..

【问题讨论】:

标签: bash tail gnu-coreutils


【解决方案1】:

我知道这是旧的,但另一个(可能更简单)的解决方案是:

watch -n 1 cat myfile.txt

【讨论】:

    【解决方案2】:

    您可以使用perl 单线过滤来自tail -f 的输出

    例如

    tail -f myfile.txt 2>&1 | perl -ne 'if (/file truncated/) {system 'clear'; print} else {print}'
    

    【讨论】:

    • 如果文件内容碰巧有“文件被截断”的文字,这个方法也会清屏。
    【解决方案3】:

    tailf myfile.txt

    这是命令 tailf 而不是 tail -f

    使用此命令不会在屏幕上返回截断文件

    【讨论】:

    • 这不会像我想要的那样清除屏幕上的旧内容。无论如何,Benj 的回答是有效的,谢谢..
    • 你很抱歉,我的坏事最初确实看得很清楚,然后在调查它时完全忘记了典型的一天结束错误:) 很高兴你让它按要求工作
    猜你喜欢
    • 2014-03-15
    • 2023-04-08
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 2021-12-19
    相关资源
    最近更新 更多