我是perl的新相对论,我正在尝试从日志文件中拆分和解析数据。日志文件包含备份何时成功与否的信息。
但是,在日志文件中的某一点,条目会重复,并导致解析数据的问题。如果不解析,我该如何跳过该条目?
正常进入顶部以及下面有问题的条目。
> $VAR1 = 'Thu Jul 31 00:35:00 2014';
> $VAR2 = 'Daily Backup for (Wed) Jul. 30, 2014
> STATUS: Successful Thu Jul 31 00:37:22 2014';
> VAR3 = 'Backup Completed: Thu Jul 31 00:40:07 2014
> $VAR1 = 'Fri May 16 00:35:00 2014';
> $VAR2 = 'Daily Backup for (Thu) May. 15, 2014
> STATUS: Successful Fri May 16 00:37:43 2014';
> $VAR3 = 'Daily Backup for (Thu) May. 15, 2014
> STATUS: Successful Fri May 16 00:39:54 2014';
> $VAR4 = 'Backup Completed: Fri May 16 00:42:37 2014
my $stdout = ("cat backup.log");
my @lines = split(/Backup Started: /, $stdout);
shift @lines;
foreach(@lines)
{
my @backupstarted = split(/\n\n/,$_);
my $start = $backupstarted[0];
my @types = split(/ Backup /, $backuptype);
my $type = $types[0];
my @statuses = split(/ /, $backupstatus);
$statuses[1] =~ s/\://g;
my $status = $statuses[1];
my @enddate = split(/ /, $backup);
my $end = $enddate[0];
答案 0 :(得分:0)
您的代码存在一些问题。
让我们以正常方式打开文件,首先:
open my $fh, '<', 'backup.log' or die "Couldn't open `backup.log': $!";
my %backup;
my $current_backup;
while (<$fh>) {
# when we see a new date...
# set up a new hash ref for its details
if (/^\w{3} \w{3} \d+ [0-9:]+ 20\d\d/) {
chomp;
$backup{$_} = {};
$current_backup = $_;
}
# look for other known types of lines
if ($current_backup) {
if (/^(\w+) Backup /) {
if ($backup{$current_backup}{type}) {
delete $backup{$current_backup};
$current_backup = '';
} else {
$backup{$current_backup}{type} = $1;
}
} elsif (/^STATUS: (\w+)/) {
$backup{$current_backup}{status} = $1;
} elsif (/^Backup Completed: (.*)/) {
$backup{$current_backup}{completed} = $1;
} else {
}
}
}
close $fh;
但即便如此,如果不正常,我认为你实际想要跳过该条目。