I am trying devise Perl regex to parse command output from IBM's runmqsc utility.
Each line of output of interest contains one or more attribute/value pairs with format: "ATTRIBUTE(VALUE)". The value for an attribute can be empty, or can contain parenthesis itself. Typically, a maximum of two attribute/value pairs appear on a given line, so the regex is written under this assumption.
Example input to Perl RE:
CHANNEL(TO.IPTWX01) CHLTYPE(CLUSRCVR)
DISCINT(6000) SHORTRTY(10)
TRPTYPE(TCP) DESCR( )
LONGTMR(1200) SCYEXIT( )
CONNAME(NODE(1414)) MREXIT( )
MREXIT( ) CONNAME2(SOME(1416))
TPNAME( ) BATCHSZ(50)
MCANAME( ) MODENAME( )
ALTTIME(00.41.56) SSLPEER()
CONTRIVED() ATTR (00-41-56)
CONTRIVED() DOCTORED()
MSGEXIT( )
I have the following Perl code to capture each attribute/value pair.
Perl Code
my $resplit = qr/\s+([^\s]+(?:\([^)]*\))?)\s?/;
while ( <IN2> )
{ s/[\s\r\n]+$//;
if ( m/^\s(?:$resplit)(?:$resplit)?$/ )
{ my ($one,$two) = ($1,$2);
print "one: $one, two: $two\n";
}
}
Here's the output when the above code is applied to sample input:
one: CHANNEL(TO.IPTWX01), two: CHLTYPE(CLUSRCVR) one: DISCINT(6000), two: SHORTRTY(10) one: TRPTYPE(TCP), two: DESCR( ) one: LONGTMR(1200), two: SCYEXIT( ) one: CONNAME(NODE(1414)), two: MREXIT( ) one: MREXIT( ), two: CONNAME2(SOME(1416)) one: TPNAME( ), two: BATCHSZ(50) one: MCANAME( ), two: MODENAME( ) one: ALTTIME(00.41.56), two: SSLPEER() one: CONTRIVED(), two: ATTR(00-41-56) one: CONTRIVED(), two: DOCTORED() one: MSGEXIT(, two: )
This works great with the exception of the last line in the output above. I'm really struggling to figure out how to modify the above expression $resplit to capture the last case.
Can anyone offer any ideas/suggestions on how to make this work or another approach?