in reply to Reformat command output inline
G'day RenMcCourtey,
As a more general solution to those already provided, when confronted with blocks of data that are to be processed as a single unit, read each block as a single record by localising $/, the input record separator. (See "perlsub: Temporary Values via local()" and "perlvar: Variables related to filehandles".)
Here's how you might do this with your posted data and requested output format:
#!/usr/bin/env perl -l use strict; use warnings; { local $/ = "\ndn: "; while (<DATA>) { / ^ cn: \s+ (.*?) $ .*? ^ orclnetdescstring: \s+ (.*?) $ /msx; print join '=', $1, $2; } } __DATA__ dn: distinguished_name1 cn: common_name1 orclnetdescstring: complex_address_line1 dn: distinguished_name2 cn: common_name2 orclnetdescstring: complex_address_line2 dn: distinguished_name3 cn: common_name3 orclnetdescstring: complex_address_line3
Output:
common_name1=complex_address_line1 common_name2=complex_address_line2 common_name3=complex_address_line3
— Ken
|
|---|