in reply to Re: Write to multiple files according to multiple regex
in thread Write to multiple files according to multiple regex
Thanks!
The code is huge, a little hard for me to understand every line. What I cannot figure out in your code is, how $OFH can write to different files. Its not defined anywhere is it?
My code changed a bit after the many suggestions here and now looks like that:
#!perl use strict; use warnings; use FindBin; my (@regex, $regex,$file,$outfile,$dir,$dh,@inputs,$inputs,@filehandle +s,$fh,$ofh); $dir ="$FindBin::Bin/../rxo"; opendir($dh, $dir) || die "can't opendir $dir: $!"; @inputs = readdir($dh); closedir $dh; splice @inputs, 0, 2; foreach(@inputs) { #localize the file glob, so FILE is unique to # the inner loop. local *FILE; local *OUTFILE; $file = "$FindBin::Bin/../rxo/$_"; $outfile = "$FindBin::Bin/../blocks/$_"; open(*FILE, "$file") || die; open(*OUTFILE, "> $outfile") || die; #push the typeglobe to the end of the array $fh = \*FILE; $ofh = \*OUTFILE; $regex = <$fh>; push(@regex,$regex); push(@filehandles,$ofh); } $/ = '^END$'; while(my $line = <>) { for my $i(0..$#inputs) { print {$filehandles[$i]} $line if $line =~ /$regex[$i]/; } }
My regexes look like this:
(?^:^UT A19(?:7(?:0G990800007|6CQ89200006)|8(?:0JW32900007|2PN88100001)|90DD63700001))
Basically the data is arranged in blocks like:
UT xxxxxx (some number), lets call this the entry
some data about the entry
some more data about the entry
END
UT xxxxx2 (next entry)
...
So i want to extract 1) all blocks if interest, 2) split these blocks in n files since these blocks relate to n different regexes
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Write to multiple files according to multiple regex
by roboticus (Chancellor) on Jul 21, 2015 at 20:29 UTC | |
|
Re^3: Write to multiple files according to multiple regex
by BillKSmith (Monsignor) on Jul 21, 2015 at 16:05 UTC | |
by Foodeywo (Novice) on Jul 21, 2015 at 18:46 UTC | |
by BillKSmith (Monsignor) on Jul 21, 2015 at 20:38 UTC | |
by Foodeywo (Novice) on Jul 21, 2015 at 21:08 UTC | |
by BillKSmith (Monsignor) on Jul 21, 2015 at 23:19 UTC | |
|