Perl新手在这里。我有一个日志文件,我需要解析“备份成功”和任何“错误:”条目。我尝试使用unix cat解析日志文件并将其传递给grep。我得到了我想要的信息,但我想在perl中尝试这个,并且还可以选择传递日期参数,并根据我需要的日期给我一行。
日志文件输出示例:(备份成功)
Wed Jun 09 06:14:25 2010: db2.cal.mil.mad:backup:INFO: flush-logs-time=00:00:00
Wed Jun 09 06:14:25 2010: db2.cal.mil.mad:backup:INFO: backup-time=06:14:23
Wed Jun 09 06:14:25 2010: db2.cal.mil.mad:backup:INFO: backup-status=Backup succeeded
Wed Jun 09 06:14:25 2010: db2.cal.mil.mad:backup:INFO: 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 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代码和/或想法,以便开始使用。我很感激你的帮助。谢谢。
答案 0 :(得分:2)
#!/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;
}
}
答案 1 :(得分:1)
这是一个简单的脚本。要扫描的文件名和目标日期是硬编码的。匹配打印到STDOUT。
顺便说一句,这段代码完全未经测试。我在浏览器的文本框中输入了它。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;
}
您可以使用Getopt::Long获取文件名和目标日期。
使用更强大的日期/时间解析和比较方案是个好主意。 DateTime和朋友是非常好的,强大的日期操作模块。检查一下。
如果您处理大量数据并且需要提高效率,则可以避免以多种方式在任何地方复制$line
。
为了将来参考,如果您发布一些代码,您会得到更好的回复