I know that this question has been asked several times in this forum, I checked the answers to most of them but still m not able to make it work for my input file:
Input file:
flow items
{
Flow Item1
{
Result -2
{
line1;
line2;
line3;
}
Result 0
{
line4;
START_TEXT blah blah blah;
blah blah blah_END_TEXT;
line4;
}
Result 1
{
line5;
START_TEXT foo foo foo;
line5;
}
Result 2
{
line6;
START_TEXT blah1 blah1 blah1;
blah1 blah1 blah1_END_TEXT;
line7;
}
}
Flow item2
{
Result -2
{
line8;
line9;
line10;
}
Result 0
{
line11;
START_TEXT blah2 blah2 blah2;
blah2 blah2 blah2_END_TEXT;
line12;
}
Result 1
{
line13;
START_TEXT foo1 foo1 foo1;
line14;
}
}
}
Output expected:
START_TEXT blah blah blah;
blah blah blah_END_TEXT;
START_TEXT blah1 blah1 blah1;
blah1 blah1 blah1_END_TEXT;
START_TEXT blah2 blah2 blah2;
blah2 blah2 blah2_END_TEXT;
So basically there are multiple occurrences of START_TEXT which are unwanted (Those one's which do not have END_TEXT in the same Result block, for example:
Result 1
{
line5;
START_TEXT foo foo foo;
line5;
}
Code that I have been trying (looking into all .txt files in the work directory and searching for the mutiline pattern:
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
use File::Find;
my $file_pattern ="txt";
find(\&d, cwd);
sub d {
my $file = $File::Find::name;
$file =~ s,/,\\,g;
return unless -f $file;
return unless $file =~ /$file_pattern/;
open F, $file or print "couldn't open $file\n" && return;
open $fout, ">>", "Result.txt";
while (<F>) {
$string =~ /(START_TEXT.*?)END_TEXT/s;
print $fout "$string\n";;
}
close F;
close $fout;
}
This returns me only few newline characters. Please help.