2014-10-31 55 views
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); 
    } 

} 

我無法提取的格式,我可以比較日期,我不確定如何與當前日期進行比較。

+1

是[時間::件(http://p3rl.org/Time::Piece)存在? – choroba 2014-11-01 00:22:01

+0

或[Time :: ParseDate](http://search.cpan.org/~muir/Time-modules-2003.0211/lib/Time/ParseDate.pm)? – 2014-11-01 01:55:23

回答

2

你想要做更復雜的東西與日期時,過濾器或您提取的日誌條目?處理日期和範圍可能會很棘手,但您似乎只是在尋找特定的日期字符串(即「當前日期」)。

perl -MTime::Piece -nE'$t=localtime; $tdate=$t->mday."-".$t->monname."-".$t->year; 
say if m/^\[$tdate/' logs.txt 

輸出:

[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: "" 
2

嘗試在Perl下面的代碼:

代碼:

use strict; 
use warnings; 
use Time::Piece; 

sub Dt_change 
{ 
    my $date_var=$_[0]; 
    my $temp; 
    my %Month=(1 => 'Jan', 2 => 'Feb', 3 => 'Mar', 4 => 'Apr' , 5 => 'May' ,6 => 'Jun' , 7 => 'Jul', 8 => 'Aug' ,9 => 'Sep', 10 => 'Oct', 11 => 'Nov' , 12 => 'Dec'); 
    if($date_var=~/^(\d+)\/0?(\d+)\/(\d+)$/) 
     { 
     my $day=$1; 
     my $year=$3; 
     my $month=$2;   
     $temp="$day" . "\-" . "$Month{$month}" . "\-" . "$year"; 
     } 
     return $temp  
    } 

my $logFile = $ARGV[0];  
open my $fh,'<',$logFile or die "Couldn't open the log file $logFile:$!";   
my $t = localtime; 
my $date = $t -> dmy("/"); 
my $dateformat = "\[" . Dt_change($date); 
my $fulldateformat = "\[" . Dt_change($date) . " " . $t -> hms . "\]"; 
$dateformat = quotemeta($dateformat); 
print "Current date log entries:\n"; 
while(<$fh>) 
{ 
    if($_ =~ /$dateformat/is) 
     { 
     print "line number $. -> $_\n"; 
     } 
    } 
close($fh); 

用法:

perl filename.pl logfile