0
我有以下格式的日誌條目的日誌條目:提取當前日期
[29-Oct-2014 06:06:08] NOTICE: fpm is running, pid 104151
[29-Oct-2014 06:06:26] NOTICE: ready to handle connections
[29-Oct-2014 06:07:14] NOTICE: Terminating ...
[29-Oct-2014 06:07:14] NOTICE: exiting, bye-bye!
[31-Oct-2014 06:07:16] NOTICE: fpm is running, pid 104871
[31-Oct-2014 06:07:36] NOTICE: ready to handle connections
[31-Oct-2014 06:41:48] WARNING: [pool blah.com] child 105502 said into stderr: "NOTICE: PHP message: "
[31-Oct-2014 06:41:48] WARNING: [pool blah.com] child 105502 said into stderr: ""
我只是想打印的當前日期(10月31日)的條目。
我一直在玩下面的腳本,但我不能得到它的工作:
use strict;
use Logwatch ':dates';
use Time::Local;
use POSIX qw(strftime);
my $date_format = '%d-%b-%Y %H:%M:%S';
my $filter = TimeFilter($date_format);
# we do not use any Date:: package (or strptime) as they are probably not available
my %month2num = (Jan => 0, Feb => 1, Mar => 2, Apr => 3,
May => 4, Jun => 5, Jul => 6, Aug => 7,
Sep => 8, Oct => 9, Nov => 10, Dec => 11);
# counting messages
while(<>) {
my $line = $_;
# skipping messages that are not within the requested range
next unless $line =~ /^\[($filter)\]/o;
$1 =~ /(\d+)-(\w+)-(\d+) (\d+):(\d+):(\d+)/;
my $time;
{
# timelocal is quite chatty
local $SIG{'__WARN__'} = sub {};
$time = timelocal($6, $5, $4, $1, $month2num{$2}, $3-1900);
}
}
我無法提取的格式,我可以比較日期,我不確定如何與當前日期進行比較。
是[時間::件(http://p3rl.org/Time::Piece)存在? – choroba 2014-11-01 00:22:01
或[Time :: ParseDate](http://search.cpan.org/~muir/Time-modules-2003.0211/lib/Time/ParseDate.pm)? – 2014-11-01 01:55:23