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>;
     }
}

(この場合は 2 として) に一致し、コマンド (この場合) とを 2 つの個別の変数としてExit status保存するように、これをどのように行うべきかについての入力が必要です。874923Exit Time

私はperlの初心者なので、私を訂正してください。

4

3 に答える 3

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 に答える