Linux 如何连续显示文件的最后几行内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14604397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:51:41  来源:igfitidea点击:

how to continuously display a file of its last several lines of contents

linuxunixsplitscreentail

提问by Wen_CSE

I am trying to find a Unix command (combination, maybe) on how to continuously display a file of its last several lines of contents. But during this displaying, I want some of the top lines are always displayed on the screen top when the rolling contents reach the screen top.

我试图找到一个关于如何连续显示文件的最后几行内容的 Unix 命令(可能是组合)。但是在此显示过程中,我希望滚动内容到达屏幕顶部时始终在屏幕顶部显示一些顶部行。

Is that possible?

那可能吗?

(1) Suppose I have file, "job.sta", the first 2 lines are: job name, John's job on 2013-Jan-30,... Tab1, Tab2, Tab3 0, 1, 2, 1, 90, 89 2, 89, 23 ...

(1) 假设我有文件“job.sta”,前两行是:job name, John's job on 2013-Jan-30,... Tab1, Tab2, Tab3 0, 1, 2, 1, 90, 89 2, 89, 23 ...

(2) This file is on its running, its contents are growing, and I don't know what line it's going to end.

(2) 这个文件在运行,内容越来越多,不知道哪一行结束。

(3) So I want to display (always) the first 2 lines when using tail command, when the updating contents reaches a Unix shell screen top. I am using PuTTY at the moment.

(3) 所以我想在使用 tail 命令时(总是)显示前 2 行,当更新内容到达 Unix shell 屏幕顶部时。我目前正在使用 PuTTY。

Reference: http://www.unix.com/unix-dummies-questions-answers/172000-head-tail-how-display-middle-lines.html

参考:http: //www.unix.com/unix-dummies-questions-answers/172000-head-tail-how-display-middle-lines.html

采纳答案by William Pursell

This will update every 2 seconds rather than whenever data is written to the file, but perhaps that's adequate:

这将每 2 秒更新一次,而不是在数据写入文件时更新一次,但这也许就足够了:

watch 'head -n 2 job.sta; tail job.sta'

回答by sameer karjatkar

You try the following combination tail -f filename | head -2

你试试下面的组合tail -f filename | 头-2

回答by user000001

I use this script to achieve the effect you describe

我用这个脚本来实现你描述的效果

!#/bin/bash
while [ 1 ]; do ls -lt data | head -n 30; sleep 3; echo -en "$(tput cuu 30; tput ed)"; done

This runs a command in a loop, and deletes the last lines of the output from the screen before each iteration.

这会在循环中运行一个命令,并在每次迭代之前从屏幕中删除输出的最后几行。

In your case it would look something like

在你的情况下,它看起来像

!#/bin/bash

while [ 1 ] ;# loop forever
do 
    head -n 2 job.sta ;# display first 2 lines 
    tail -n 30 job.sta ;# display last 30 lines from file
    sleep 3 ;# wait a bit
    echo -en "$(tput cuu 32; tput ed)" ;# delete last 32 lines from screen
done

Of course it is a bit ugly before your file reaches 30 lines

当然在你的文件达到30行之前有点难看

Hope that helps

希望有帮助

回答by choroba

You can use screento simulate the expected behaviour:

您可以使用screen来模拟预期的行为:

  1. Run screen, press Space.

  2. Press Ctrl+afollowed by Sto split the screen.

  3. Resize the top window by pressing Ctrl+afollowed by :resize 4.

  4. In the prompt in the top window, enter head -n2 file.

  5. Move to the bottom window by pressing Ctrl+afollowed by Tab.

  6. Start a new screen session by pressing Ctrl+afollowed by c.

  7. In the new prompt, enter tail -f file.

  1. 运行screen,按Space

  2. Ctrl+a然后按S分割屏幕。

  3. Ctrl+a然后按 调整顶部窗口的大小:resize 4

  4. 在顶部窗口的提示中,输入head -n2 file

  5. Ctrl+a然后按 移到底部窗口Tab

  6. Ctrl+a然后按 开始新的屏幕会话c

  7. 在新提示中,输入tail -f file.

回答by Andy Lester

You want the multitailprogram, which not only does the split screen stuff but will also do color-coding.

您需要该multitail程序,该程序不仅可以执行分屏操作,还可以执行颜色编码。

http://www.vanheusden.com/multitail/

http://www.vanheusden.com/multitail/

回答by wacko413

I use this function all the time to monitor a log file in another terminal window.

我一直使用此功能来监视另一个终端窗口中的日志文件。

tail -f <filename>

I recommend taking it a step forward to look for particular text in the log. Great if you are only interested in seeing some particular entry being written to the file.

我建议向前迈出一步,在日志中查找特定文本。如果您只对查看写入文件的某些特定条目感兴趣,那就太好了。

tail -f <filename> | grep <keyword or pattern>