【发布时间】:2012-12-30 21:41:12
【问题描述】:
我正在尝试构建一个 shell 脚本来监控一些日志文件。 我正在使用这样的命令:
tail -f /var/somelog | grep --line-buffered " some test and p l a c e h o l d e r" | cut -f 3,4,14 -d " "
日志文件如下:
some test and p l a c e h o l d e r 3
some test and p l a c e h o l d e r 4
some test and p l a c e h o l d e r 5
some test and p l a c e h o l d e r 6
等等.. 我的问题是命令的输出不显示最后一行
some test and p l a c e h o l d e r 6
直到行
some test and p l a c e h o l d e r 7
被添加到日志中。
我希望我能明确我的问题。谁能帮我解决这个问题? 谢谢你:)
【问题讨论】:
-
也许日志行没有以换行符结束,而是以一个换行符开始?在这种情况下,直到第 7 行开始并提供该换行符,第 6 行才真正完成。如果您查看显示所有字符的日志文件,您是否在最后一行的末尾看到换行符?
-
使用
od -c /var/somelog查找尾随换行符(“\n”)。 -
您是否看到与
tail -f相同的输出?如果是这种情况,那么@David 已经正确指出了这一点。 -
我现在不在工作,但如果我运行 tail -f 我可以看到完整的日志。今晚我将尝试您的解决方案并及时通知您:)
-
我猜你会看到
tail -f的完整输出(包括最后一行),甚至tail -f | grep --line-buffered "pattern",但是一旦你将cut引入管道,你就会丢失最后一行.