-1

I have written a Perl script which parses the output of a command, looks for the task name in the command output and prints the task name.

Command output looks like the below

Here task name is 'mutipleregexpression'. But part of the word 'pression' gets printed in the new line.

Task Name:                                                  multipleregex
pression

This is the regex that im currently using in my code:

`$line =~ /(.*):\s*(.+)/` 

The above regex doesn't capture part of the task name that gets printed in the new line. It only captures 'multipleregex' it doesn't capture 'pression' since pression gets printed in the new line. i see the following error

Use of uninitialized value $result in concatenation (.) or string at  line 114.

So my question is what is the change that i need to make to the regularexpression so that it captures 'pression' as well

function for capturing the task name

sub get_status {
  my ($self) = @_;

  my $taskname;
  my %result;
  my $line;

  $self->{'cmd'} = # command goes here;

  # execute the command
  $self->execute($self->{'cmd'});
  $self->{'stdout'} = $self->get_stdout();

  my $count = 0;

  # traverse through the output to parse the output

  foreach $line (@{ $self->{'stdout'} }) {
    chomp $line;

    if ( $line =~ m/^Task\sName\:\s+(\w+)/msx ) {
      $taskname = $1;
      $count    = 0;
    }
    elsif ($line =~ /(.*):\s*(.+)/) {
      my $key   = $1;
      my $value = $2;
      $result{$taskname}{ ++$count } = $value;
    }
  }

  return \%result;
}

Function call :

my $result = $stats->get_status('multipleregexpression', '1');
       INFO('Status of the task is :' . $result );

Output :

Use of uninitialized value $result in concatenation (.) or string at  line 114.
20150317T194029   INFO    Status of the task is :
user3587025
  • 173
  • 1
  • 4
  • 17
  • You don't seem to have shown any Perl code that is relevant to your question. `_get_status` appears to be a private method of a class that you haven't mentioned, and doesn't create any output. Please tell us a *lot* more, and at least include a sample of the the data that you're matching—presumably the contents of the array referred to by `$self->{stdout}`? – Borodin Mar 18 '15 at 00:24
  • @Borodin added the function call and the output. – user3587025 Mar 18 '15 at 01:01
  • @Borodin Et al i have edited my question .Let me know if its still unclear. – user3587025 Mar 18 '15 at 15:41
  • How do you know that `pression` is a continuation of the task name, and not the start of a different data line? Is it the absence of a colon? Since '`Task Name:`' contains a space, it isn't simply 'the first word ends in a colon' (unlike email headers, for example; the tags for those contain no spaces). You need to be able to identify whether a given input line is a continuation of the previous line by some means or another. With that done (identifying the complete 'line'), the regex to remove the newline in the data is not too onerous. – Jonathan Leffler Mar 18 '15 at 18:15
  • @JonathanLeffler different data line (if any ) gets printed in the next line.Im not really sure what i need to add to my regex to capture the part of the task name thats gets printed in the next line. – user3587025 Mar 18 '15 at 20:42

1 Answers1

0

I don't know the language as well, but you can try this logic:

var current_line = empty string.

foreach LINE in lines {
   if LINE contains ":" {
      then
         1. Save or do something with last line (current_line).
         2. LINE is the start of a new line (current_line = what comes after ":").
   }
   else {
      then 
         1. LINE is a continuation of last line 
            (current_line = current_line + LINE).
   }
}

Hope it helps.