需要过滤日志以搜索最近 5 分钟的行

发布于 2024-11-03 13:24:27 字数 241 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦初启 2024-11-10 13:24:27

下面是一个简单的 Perl 脚本,它迭代文件并打印时间戳在执行开始时间 5 分钟内的每一行。为了提高效率,并假设这些行按时间戳顺序排列,您可以修改它以在遇到第一个可打印行时设置一个布尔标志,并跳过从该点开始的测试。

#!/usr/bin/perl
use POSIX qw(mktime);

$now = time();
while(<>)
{
    ($yy,$mm,$dd,$h,$m,$s,$t) = /^(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+),(\d+)/;
    $t = mktime($s+$t/1000, $m, $h, $dd, $mm-1, $yy-1900);
    print "$_" if ($t >= $now-300);
}

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.

#!/usr/bin/perl
use POSIX qw(mktime);

$now = time();
while(<>)
{
    ($yy,$mm,$dd,$h,$m,$s,$t) = /^(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+),(\d+)/;
    $t = mktime($s+$t/1000, $m, $h, $dd, $mm-1, $yy-1900);
    print "$_" if ($t >= $now-300);
}
全部不再 2024-11-10 13:24:27

根据您最新的评论,我认为您有兴趣了解如何查找日志中最后的时间戳以及之前 5 分钟的条目。

来替换

$now = time();

我认为 Jim Garrison 的解决方案可以通过修补

open F, "<server.log" or die $!;
seek F,-1000,2; # set pos to last 1000 bytes
my @f = <F>;
$_ = $f[$#f];
($yy,$mm,$dd,$h,$m,$s,$t) = /^(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+),(\d+)/;
$now = mktime($s+$t/1000, $m, $h, $dd, $mm-1, $yy-1900);

$now 现在应该包含日志中的最后一个时间戳。

我估计“-1000”足够长,足以超过日志中的至少一行。如果您希望日志中包含长行,则可以将其设置得更高,但从我所看到的来看,最后一个日志条目“应该”相当短。

如果您有一个巨大的日志文件并希望提高以下搜索的性能,您可以使用估计并执行查找来查找文件中的最后(例如 1000000 个字节):

seek F, -1000000, 2;

祝您好运!

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:

$now = time();

with this:

open F, "<server.log" or die $!;
seek F,-1000,2; # set pos to last 1000 bytes
my @f = <F>;
$_ = $f[$#f];
($yy,$mm,$dd,$h,$m,$s,$t) = /^(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+),(\d+)/;
$now = mktime($s+$t/1000, $m, $h, $dd, $mm-1, $yy-1900);

$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:

seek F, -1000000, 2;

Good luck!

新一帅帅 2024-11-10 13:24:27

使用 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

月下凄凉 2024-11-10 13:24:27

如何修改脚本以使其与下面的日志一起使用

Dec 18 09:41:18 sd
Dec 18 09:46:29 sds
Dec 18 09:48:39 sds
Dec 18 09:48:54 sds
Dec 18 09:54:47 sds
Dec 18 09:55:33 sds
Dec 18 09:55:38 sds
Dec 18 09:57:58 sds
Dec 18 09:58:10 sds
Dec 18 10:00:50 sdsd
Dec 18 10:03:43 sds
Dec 18 10:03:50 sdsd
Dec 18 10:04:06 sdsd
Dec 18 10:04:15 sdsd
Dec 18 10:14:50 wdad
Dec 18 10:19:16 sdadsa
Dec 18 10:19:23 dsds
Dec 18 10:21:03 sadsd
Dec 18 10:22:54 adas
Dec 18 10:27:32 qadad

How to modify your script to make it work with the logs below

Dec 18 09:41:18 sd
Dec 18 09:46:29 sds
Dec 18 09:48:39 sds
Dec 18 09:48:54 sds
Dec 18 09:54:47 sds
Dec 18 09:55:33 sds
Dec 18 09:55:38 sds
Dec 18 09:57:58 sds
Dec 18 09:58:10 sds
Dec 18 10:00:50 sdsd
Dec 18 10:03:43 sds
Dec 18 10:03:50 sdsd
Dec 18 10:04:06 sdsd
Dec 18 10:04:15 sdsd
Dec 18 10:14:50 wdad
Dec 18 10:19:16 sdadsa
Dec 18 10:19:23 dsds
Dec 18 10:21:03 sadsd
Dec 18 10:22:54 adas
Dec 18 10:27:32 qadad
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文