需要过滤日志以搜索最近 5 分钟的行
2011-04-13 00:09:07,731 INFO [STDOUT] 04/13 00:09:07 信息...
大家好。我会发布一些我的代码,但我什至认为它不值得发布。我想做的是,我有一个包含上述行的日志文件。我需要做的是获取最后几行时间戳,并保留最后 5 分钟的所有行(而不是最后 200 行或其他......这会更容易)。有人可以帮忙吗?我在网上搜索了一些不错的提示,但仍然没有任何进展,并且非常沮丧。谢谢!
2011-04-13 00:09:07,731 INFO [STDOUT] 04/13 00:09:07 Information...
Hi everyone. I would post some of my code, but I don't even think it's worthy of posting. What I'm trying to do is that I've got a log file with lines like above. What I need to do is take the last lines timestamp, and keep all the lines from the last 5 minutes (rather than the last 200 lines or whatever....which would be easier). Could anyone help? I've searched the web, some decent tips, but still nothing going and frustrated as hell. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
下面是一个简单的 Perl 脚本,它迭代文件并打印时间戳在执行开始时间 5 分钟内的每一行。为了提高效率,并假设这些行按时间戳顺序排列,您可以修改它以在遇到第一个可打印行时设置一个布尔标志,并跳过从该点开始的测试。
Here's a simple Perl script that iterates over the file and prints every line whose timestamp is within 5 minutes of the time at the start of execution. For more efficiency, and assuming that the lines are in timestamp order, you could modify this to set a boolean flag when it encounters the first printable line and skip the testing from that point forwards.
根据您最新的评论,我认为您有兴趣了解如何查找日志中最后的时间戳以及之前 5 分钟的条目。
来替换
我认为 Jim Garrison 的解决方案可以通过修补
:
$now
现在应该包含日志中的最后一个时间戳。我估计“-1000”足够长,足以超过日志中的至少一行。如果您希望日志中包含长行,则可以将其设置得更高,但从我所看到的来看,最后一个日志条目“应该”相当短。
如果您有一个巨大的日志文件并希望提高以下搜索的性能,您可以使用估计并执行查找来查找文件中的最后(例如 1000000 个字节):
祝您好运!
I take it by your latest comment that you are interested in finding out how to find the timestamp that is last in your log, and the entries that are 5 minutes before that.
I think Jim Garrison's solution could be patched to replace this:
with this:
$now
should now contain the last timestamp in the log.I approximated "-1000" to be long enough to go past at least one line in the log. You could set it much higher if you expect to have long lines in the log, but from what I saw, the last log entry "should" be fairly short.
If you have a huge log file and want to increase performance in the following search, you can use an estimation and perform a seek to find the last, say, 1000000 bytes in the file with:
Good luck!
使用 regexp scrap: 00:09:07 迭代所有行,并检查当前时间(本地时间等...)。
如果文件包含来自不同日期的条目,则还使用正则表达式获取日期,并再次使用 位置时间
Iterate over all the lines, using regexp grab: 00:09:07, and check against current time (localtime, etc...).
if the file contains entries from different dates, then also grab the dates using regexp, and again compare using the output of locatime
如何修改脚本以使其与下面的日志一起使用
How to modify your script to make it work with the logs below