perl帮助根据时间输入解析日志文件
我是新手。我有一个日志文件,需要解析出“备份成功”和任何“错误:”条目。我尝试使用unix cat解析日志文件,并将其传输到grep。我得到了我想要的信息,但是我想在perl中尝试一下,并且还可以选择传递一个日期参数,并根据我需要的日期给我行perl帮助根据时间输入解析日志文件,perl,Perl,我是新手。我有一个日志文件,需要解析出“备份成功”和任何“错误:”条目。我尝试使用unix cat解析日志文件,并将其传输到grep。我得到了我想要的信息,但是我想在perl中尝试一下,并且还可以选择传递一个日期参数,并根据我需要的日期给我行 Wed Jun 09 05:00:03 2010: rip1.mil.mad:backup:ERROR: mysql-zrm appears to be already running for this backupset Wed Jun 09 05:00
Wed Jun 09 05:00:03 2010: rip1.mil.mad:backup:ERROR: mysql-zrm appears to be already running for this backupset
Wed Jun 09 05:00:03 2010: rip1.mil.mad:backup:ERROR: If you are sure mysql-zrm is not running, please remove the file /etc/mysql-zrm/rip1.mail.mad/.mysql-zrm.pid and restart mysql-zrm
Wed Jun 09 06:14:25 2010: db2.cal.mil.mad:backup:INFO: backup-status=Backup succeeded
日志文件输出示例:(备份成功)
日志文件输出示例:(错误:)
**我想要一个文本和/或电子邮件与此信息。像这样,但是可以选择我需要的日期
Wed Jun 09 05:00:03 2010: rip1.mil.mad:backup:ERROR: mysql-zrm appears to be already running for this backupset
Wed Jun 09 05:00:03 2010: rip1.mil.mad:backup:ERROR: If you are sure mysql-zrm is not running, please remove the file /etc/mysql-zrm/rip1.mail.mad/.mysql-zrm.pid and restart mysql-zrm
Wed Jun 09 06:14:25 2010: db2.cal.mil.mad:backup:INFO: backup-status=Backup succeeded
如果您愿意,请为我提供一些perl代码和/或开始使用的想法。我将非常感谢你的帮助。谢谢。这里有一个简单的脚本。要扫描的文件名和目标日期是硬编码的。匹配项打印到标准输出 顺便说一句,这段代码完全没有经过测试。我在浏览器的文本框中输入了它
use strict;
use warnings;
my $logpath = './bar/log';
my $target = 'Jun 09 2010';
open my $fh, '<', $logpath or die "Error opening $logpath $!\n";
while (my $line = <$fh> ) {
next unless date_match( $target, $line );
next unless my $result = got_error($line) // got_backup($line);
print $result;
}
sub got_backup {
my $line = shift;
return unless $line =~ /backup-status=Backup succeeded/;
return $line;
}
sub got_error {
my $line = shift;
return unless $line =~ /:ERROR:/;
return $line;
}
# Take a line and a target date. Compare the date derived from the line to
# the target, and returns true if they match.
# Also always returns true if target is not defined
sub date_match {
my $target = shift;
my $line = shift;
return 1 unless defined $target; # Always true if target is undefined.
# Where did that god-awful date format come from? Yech.
my $date = extract_date($line);
return $date eq $target;
}
# Simple extract of date using split and join with extra variables
# to make it newbie friendly.
# IMO, it would be a good idea to switch to using DateTime objects and
# DateTime::Format::Strptime
sub extract_date {
my $line = shift;
my @parts = split /:/, $line;
my $date = join ':' @parts[0..2];
@parts = split /\s+/, $date;
$date = @parts[1,2,4];
return $date;
}
使用严格;
使用警告;
我的$logpath='./bar/log';
我的美元目标=‘2010年6月9日’;
打开我的$fh,“#”/usr/bin/perl
#用法示例:2010年6月9日
严格使用;
使用警告;
我的($mon,$day,$year)=($ARGV[0]、$ARGV[1]、$ARGV[2]);
打开(FH,“<$ARGV[3]”)或死亡“无法打开日志文件$ARGV[3]:$!\n”;
while(我的$line=){
如果($line=~/.$mon$day\d{2}:\d{2}:\d{2}$year:.*(错误:|备份成功)/){
打印$行;
}
}
完美!这很有效。正是我需要的。谢谢你的帮助!
use strict;
use warnings;
my $logpath = './bar/log';
my $target = 'Jun 09 2010';
open my $fh, '<', $logpath or die "Error opening $logpath $!\n";
while (my $line = <$fh> ) {
next unless date_match( $target, $line );
next unless my $result = got_error($line) // got_backup($line);
print $result;
}
sub got_backup {
my $line = shift;
return unless $line =~ /backup-status=Backup succeeded/;
return $line;
}
sub got_error {
my $line = shift;
return unless $line =~ /:ERROR:/;
return $line;
}
# Take a line and a target date. Compare the date derived from the line to
# the target, and returns true if they match.
# Also always returns true if target is not defined
sub date_match {
my $target = shift;
my $line = shift;
return 1 unless defined $target; # Always true if target is undefined.
# Where did that god-awful date format come from? Yech.
my $date = extract_date($line);
return $date eq $target;
}
# Simple extract of date using split and join with extra variables
# to make it newbie friendly.
# IMO, it would be a good idea to switch to using DateTime objects and
# DateTime::Format::Strptime
sub extract_date {
my $line = shift;
my @parts = split /:/, $line;
my $date = join ':' @parts[0..2];
@parts = split /\s+/, $date;
$date = @parts[1,2,4];
return $date;
}
#!/usr/bin/perl
# usage example: <this script> Jun 09 2010 <logfile>
use strict;
use warnings;
my ($mon,$day,$year) = ($ARGV[0],$ARGV[1],$ARGV[2]);
open(FH,"< $ARGV[3]") or die "can't open log file $ARGV[3]: $!\n";
while (my $line = <FH>) {
if ($line =~ /.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(ERROR:|Backup succeeded)/) {
print $line;
}
}