in reply to parsing multi-line output from a cli command

So your summary comes second, and can be multiline. Hmm. Anyway, what distinguishes between the first and the next lines, is the leading whitespace on the latter.

What I'd do is something like the following — which doesn't quite work according to your minimal spec, but is better, IMHO:

my(@records, $ref); while(<DATA>) { chomp; if(my @fields = /^(\S+): \[(.+?)\] \[(.+?)\] \[(.+?)\] \[(.+?)\]\s +*$/) { my %record; @record{'ticket#', qw(customer status priority owner)} = @fiel +ds; push @records, $ref = \%record; } elsif(s/^\s+//) { if(defined $ref->{summary}) { $ref->{summary} .= "\n$_"; } else { $ref->{summary} = $_; } } else { warn "Oops: no match in: $_\n"; } } use Data::Dumper; print Dumper \@records; __DATA__ fabx-t160: [ggurudut] [UNAN/OWNR] [C2] [kelrod] navicli chglun -l 3 -name "newname" doesn't work fabx-t161: [dozone] [UNAN/OWNR] [C2] [dchoi] The GUI needs to hide the CPP SEs from the unimported list fabx-t162: [haurora] [UNAN/OWNR] [C1] [glade] Cisco hardware related bug :idprom error on cisco switch on loading + 0.1.5.5 salagent (This line won't match)
which produces the output (the first line is a warning, which goes to STDERR):
Oops: no match in: (This line won't match) $VAR1 = [ { 'summary' => 'navicli chglun -l 3 -name "newname" doesn\'t + work', 'status' => 'UNAN/OWNR', 'customer' => 'ggurudut', 'ticket#' => 'fabx-t160', 'owner' => 'kelrod', 'priority' => 'C2' }, { 'summary' => 'The GUI needs to hide the CPP SEs from the u +nimported list', 'status' => 'UNAN/OWNR', 'customer' => 'dozone', 'ticket#' => 'fabx-t161', 'owner' => 'dchoi', 'priority' => 'C2' }, { 'summary' => 'Cisco hardware related bug :idprom error on +cisco switch on loading 0.1.5.5 salagent', 'status' => 'UNAN/OWNR', 'customer' => 'haurora', 'ticket#' => 'fabx-t162', 'owner' => 'glade', 'priority' => 'C1' } ];

Perhaps a tiny bit of explanation is in order. When the first line of a record is encountered, a fresh hash ref, a new record holding the data for the first line, is pushed onto the global memory array @records. At the same time, a reference to this latest record is kept in the variable $ref. We can use that ref to still modify the original record, even while it's already on the array. So I use it to append more summary lines to the hash item for 'summary'.

Replies are listed 'Best First'.
Re: Re: parsing multi-line output from a cli command
by Not_a_Number (Prior) on Jan 23, 2004 at 20:29 UTC

    Re this bit:

    elsif(s/^\s+//) { if(defined $ref->{summary}) { $ref->{summary} .= "\n$_"; } else { $ref->{summary} = $_; }

    You can reduce it to:

    elsif(s/^\s+//) { $ref->{summary} .= "\n$_"; }

    dave

      Except now your summary will always start with a newline. Which is the reason for my elaborate scheme. :)

        Ooops! Hadn't thought of that :-( (ie that the OP might have wanted 'salagent' (whatever that means) on a new line.

        Good catch++. A lot of the other answers to this node need adjusting accordingly, if such is the case.

        dave