Text::Balanced should do what you need.
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
| [reply] |
Shocked I am, shocked! No one remembered '..' ?
while( <DATA> ) {
if( m/#BG/..m/#EN/ ) {
print "$.: $_";
}
}
__DATA__
blah blah blah
#BG blah blah blah
blah blah blah
#EN
blah blah blah
blah #BG blah #EN blah
blah blah blah
will output
2: #BG blah blah blah
3: blah blah blah
4: #EN
6: blah #BG blah #EN blah
and as a one-liner Perl
perl -ne "print if m/#BG/..m/#EN/;" anon03.dat
| [reply] [d/l] |
If your files aren't too big, then this might get you started.
perl -0777 -e"print <stdin> =~ m[( \Q$ARGV[0]\E .*? \Q$ARGV[1]\E )]sxg
+" "#BG" "#EN" < yourinputfile >youroutputfile
Note: The quoting will be different for *nix. Using 's instead of "s will probably work, and you may not need to quote your start and end strings depending upon what they contain and which shell you are using.
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.
| [reply] [d/l] |
#!/usr/bin/perl -w
my $file = shift;
my $start = shift;
my $end = shift;
my $outfile = "$file.out";
open(IN, "<$file") || die "Can't open $file!\n";
open(OUT, ">$outfile") || die "Can't open $outfile!\n";
while(<IN>) {
if (/$start(.*)$end/) {
print OUT $1;
}
}
With your less-than-ample description, that's the best I can do in short order. Note that this code is completely *untested*, but shouldn't need too much messaging.
HTH. | [reply] [d/l] |
<massage>
#!/usr/bin/perl -w
use strict;
my $file = shift; #always!
my $start = quotemeta shift; #quote meta chars
my $end = quotemeta shift; #quote meta chars
my $outfile = "$file.out";
open(IN, "<$file") || die "Can't open $file!\n";
open(OUT, ">$outfile") || die "Can't open $outfile!\n";
my $file_contents = do {local $/; <IN>}; #slurp!
while ($file_contents =~ /($start.*?$end)/gs) {
print OUT $1, "\n";
}
</massage>
__infile__
blah blah blah
#BG blah blah blah
blah blah blah
#EN
blah blah blah
blah #BG blah #EN blah
blah blah blah
__infile.out__
#BG blah blah blah
blah blah blah
#EN
#BG blah #EN
-- flounder | [reply] [d/l] |