in reply to parsing multi-line output from a cli command
What I'd do is something like the following — which doesn't quite work according to your minimal spec, but is better, IMHO:
which produces the output (the first line is a warning, which goes to STDERR):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)
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 | |
by bart (Canon) on Jan 23, 2004 at 20:31 UTC | |
by Not_a_Number (Prior) on Jan 23, 2004 at 21:13 UTC |