Saturday 15 March 2014

regex - perl split file with selective data -



regex - perl split file with selective data -

i trying split big file smaller files based string within file. along filter non-required elements, selecting elements desired in list.

example input

block(a_1){ block_area : 2.6112; block_footprint : 3baa5927a22e66b0ae1214a806440f12; block_coordinates { values ("0 , 0",\ "50, 50"); } block_connection : "north"; } block(bx_q_2_1){ block_area : 2.6112; block_footprint : 3baa5927a22e66b0ae1214a806440f12; block_coordinates { values ("20 , 20",\ "20, 70"); block_connection : "south"; } block(c_2_r){ block_area : 2.6112; block_footprint : 3baa5927a22e66b0ae1214a806440f12; block_coordinates { values ("50 , 50",\ "10, 500"); block_connection : "north-west"; }

output 3 files grep block_area & block_coordinates entries sample input has lot of other info hence grep using regex.

a_1.txt

block(a_1){ block_area : 2.6112; block_coordinates { values ("0 , 0",\ "50, 50"); } }

bx_q_2_1.txt

block(bx_q_2_1){ block_area : 2.6112; block_coordinates { values ("20 , 20",\ "20, 70"); }

c_2_r.txt

block(c_2_r){ block_area : 2.6112; block_coordinates { values ("50 , 50",\ "10, 500"); }

i before helped split file

while (<>) { ($file) = m|\( (.+?) \)|x or next; open $fh, ">", "$file.txt"; print $fh $_; close $fh; }

alternately

while (<$in_fh>) { open $out_fh, '>', "$1.txt" if / block \( (\w+) \) /x; print $out_fh $_ if $out_fh; }

but not able include selective data.

regards

to output specific keywords, i'd utilize next program:

#!/usr/bin/perl utilize warnings; utilize strict; $out; while (<>) { if (my ($filename) = /block \( (.*?) \){/x) { open $out, '>', "$filename.txt" or die $!; } print {$out} $_ if ! /block_/ # header & inner values or /block_(?: area | coordinates )/x; # keywords }

it doesn't work if need skip multiline entries, though.

regex perl

No comments:

Post a Comment