当前位置:首页 > 操作系统 > Linux

linux – 如何按时间拖尾日志文件?

说我的日志文件(log.txt)是这样的

2014-01-01 22:30:30 something happened....
2014-01-01 22:30:31 something happened....
2014-01-01 22:30:41 something happened....

我想拖尾这个文件显示最后一小时的日志,并保持拖尾..

tail <some magic to specify last 1 hour> -f log.txt

然后输出是

2014-01-01 21:30:41 something happened....
...
2014-01-01 22:30:30 something happened....
2014-01-01 22:30:31 something happened....
2014-01-01 22:30:41 something happened....

有没有工具可以做到这一点?

解决方法:

你可以在oneliner中使用grep和tail的组合.

grep "2014-01-01 21:" log.txt; tail -f log.txt

它将打印从那个小时开始的所有内容,并保持拖尾.

或者您也可以使用awk打印从特定小时开始到文件结束的所有内容,并在其后继续拖尾,这将允许您在需要时拖尾几个小时.

awk '/2014-01-01 21:/' log.txt; tail -f log.txt

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐