Question

I have a log file which looks like below:

 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

I have a script which looks at a pattern and returns the below line of that matched pattern. The script is as follows:

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

I need your input regarding how should I do this, such that it matches the Exit status (as 2 in this case) and save the 874923 line along with the commands (In this case) and the Exit Time as two separate variables.

Please correct me as I am a newbie in perl.

Was it helpful?

Solution 3

With the help of hash, this is what I used:

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

OTHER TIPS

Your code could be like this:

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

Output: So this will print 'arrayref of hashrefs' for entries having exit status 2

  $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 '
          }
        ];

This is how I would do it...

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;

That should print out the following:

$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'
      }
    ];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top