1

我有一个如下所示的日志文件:

 874899 root@commands to execute some files
    Exit Status : 0
    Exit time   : Sun May  5 18:19:39 2013
 874923 root@commands to execute some files
    Exit Status : 2
    Exit time   : Sun May  5 18:19:42 2013

我有一个脚本,它查看一个模式并返回该匹配模式的下一行。脚本如下:

 open(FH,'log.txt');
 while ($line = <FH>) {
     if ($line =~ /Exit Status/) {
         print "$line";
         print scalar <FH>;
     }
}

我需要您关于我应该如何执行此操作的输入,以便它匹配Exit status(在本例中为 2)并将该874923行与命令(在本例中)和Exit Time作为两个单独的变量一起保存。

请纠正我,因为我是 perl 的新手。

4

3 回答 3

1

你的代码可能是这样的:

 use Data::Dumper;
 open(FH,'inlog.txt');

 my @stat;

 my ($exitstatus, $exitstatusval, $exittime, $exittimeval, $exitcommands);
 while ($line = <FH>) {
        if ($line =~ m/\d+\s+.*@.*/) {
            $exitcommands = $line;
        }
        if ($line =~ /Exit Status/) {
            ($exitstatus, $exitstatusval) = split(':',$line);
            next;
        }
        if ($line =~ /Exit time/ and $exitstatusval == 2) {
            ($exittime, $exittimeval) = split(': ',$line);
             push (@stat, {
                commands => $exitcommands,
                time => $exittimeval
                });
        }
}

print(Dumper(\@stat));

输出:所以这将为退出状态为 2 的条目打印 'arrayref of hashrefs'

  $VAR1 = [
          {
            'time' => 'Sun May  5 18:19:42 2013 ',
            'commands' => '874923 root@commands to execute some files '
          },
          {
            'time' => 'Sun May  4 18:19:42 2013',
            'commands' => '874613 root@commands to execute some files '
          }
        ];
于 2013-07-10T08:41:01.187 回答
0

这就是我会做的...

use Data::Dumper;

open(FH,'<','log.txt');
my $current_log;
my @logs;
while (my $line = <FH>) {
  if($line =~ /^\s*(\d+)\sroot\@(.*)/) {
    if($current_log) {
      push @logs,$current_log;
    }
    $current_log = {};
    $current_log->{pid} = $1;
    $current_log->{command} = $2;
  }
  if ($line =~ /Exit Status\s*:\s*(\d+)/) {
    $current_log->{exit_status} = $1;
  }
  if($line =~ /Exit time\s*:\s*(.+)$/) {
    $current_log->{exit_time} = $1;
  }
}
if($current_log) {
  push @logs,$current_log;
}

print Dumper \@logs;

那应该打印出以下内容:

$VAR1 = [
      {
        'exit_time' => 'Sun May  5 18:19:39 2013',
        'pid' => '874899',
        'exit_status' => '0',
        'command' => 'commands to execute some files'
      },
      {
        'exit_time' => 'Sun May  5 18:19:42 2013',
        'pid' => '874923',
        'exit_status' => '2',
        'command' => 'commands to execute some files'
      }
    ];
于 2013-07-10T01:32:37.253 回答
0

在哈希的帮助下,这就是我使用的:

use Data::Dumper;
open(FH,'inlog.txt');

my %stat;

my ($exitstatus, $exitstatusval, $exittime, $exittimeval, $exitcommands);
while ($line = <FH>) {
    if ($line =~ m/^(\d+)\s+.*@(.*)/) {
        $exitcommands = $2;
        $qbsid= $1;
    }
    if ($line =~ /Exit Status/) {
        ($exitstatus, $exitstatusval) = split(':',$line);
        next;
    }
    if ($line =~ /Exit time/ and $exitstatusval == 2) {
        ($exittime, $exittimeval) = split(': ',$line);
         $stat{$qbsid} = {
            commands => $exitcommands,
            time     => $exittimeval
            };
    }

}

  print(Dumper(\%stat));
于 2013-07-16T22:01:01.853 回答