in reply to Best way to parse multiline data?
If your files are small, then slurping and using a regex with /s and a lookahead is the easiest way:
#! perl -slw use strict; my $data = do{ local $/; <DATA> }; print "'$1'$2'\n" while $data =~ m[([a-z-]+):(.*?)\n(?=[a-z-]+:|$)]sg; __DATA__ aut-num: AS19710 as-name: ASN descr: S4R admin-c: SNE1 tech-c: SNE1 import: from AS3356 63.215.71.1 at 63.215.71.2 action pref=20; +med=50; from AS3356 63.215.86.133 at 63.215.86.134 action pref= +50; med=150; accept ANY import: from AS3847 action pref=10; accept ANY export: to AS3847 announce AS19710 export: to AS3356 announce AS19710 notify: nwcontact@email mnt-by: S4R changed: andy@email 20010502 source: LEV
At each iteration of the while loop, $1 will be the section header, and $2 the body of the section with all the whitespace intact. You can further process $2 to remove or reduce the whitespace as required.
P:\test>448390 'aut-num' AS19710' 'as-name' ASN' 'descr' S4R' 'admin-c' SNE1' 'tech-c' SNE1' 'import' from AS3356 63.215.71.1 at 63.215.71.2 action pref=20; + med=50; from AS3356 63.215.86.133 at 63.215.86.134 action pref= +50; med=150; accept ANY' 'import' from AS3847 action pref=10; accept ANY' 'export' to AS3847 announce AS19710' 'export' to AS3356 announce AS19710' 'notify' nwcontact@email' 'mnt-by' S4R' 'changed' andy@email 20010502' 'source' LEV'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Best way to parse multiline data?
by jalewis2 (Monk) on Apr 16, 2005 at 02:43 UTC |