-3

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.

kaur
  • 7
  • 3
  • try this: http://stackoverflow.com/questions/953707/in-perl-how-can-i-read-an-entire-file-into-a-string – JMP Sep 09 '14 at 01:00
  • 1
    This post is a complete mess and a shame. Your data doesn't have balanced braces; `line4` and `line13` have no trailing semicolon while all others do; your sample code tries to process all files in the current directory or any subdirectory that have `txt` in their name; you use ridiculous identifiers like `d` for your your callback, and `F` and `$fout` for your file handles. Yet you are asking us for a solution! Your first step should be to clean up your act and come back with a proper description of the status and the problem. – Borodin Sep 09 '14 at 04:49
  • sorry about that, will be careful next time. – kaur Sep 16 '14 at 20:36
  • I have corrected the input file. And also my programming is bad because I'm hardware focused in my job profile, only need to write one or two scripts here and there for data. This doesn't justify not improving my programming skills, but just a picture of my situation. I am working on getting better with scripting :) – kaur Sep 16 '14 at 20:43

1 Answers1

2

You can set the Input Record Separator to }.

use strict;
use warnings;
use autodie;

open my $fh, '<', 'input.file';

local $/ = "}";

while(<$fh>) {
    next unless my ( $cap ) = /(START_TEXT.*END_TEXT;)/s;
    $cap =~ s/\n\s*/\n/g;
    print $cap, "\n";
}

Output:

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;
jaypal singh
  • 74,723
  • 23
  • 102
  • 147