Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all!

I have a text file that's too large to be processed without doing it line by line, and yet I need to do multi-line matching. I'm trying to write a script to chop the parts out that I need (both Section 1's) Here's what I Have so far:

PERL
$insidesection = 0;
while (defined($line = <>))
{
    if($line =~ /Section.*?1/)
    {
        $insidesection = 1;
    }
    elsif($line =~ /Section.*?2)
    {
        $insidesection = 0;
    }
    if ($insidesection = 1)
    {
        print $line;
    }
}


Thanks in advance!

Still doesn't seem to work. Anyone see where i'm going wrong?

-Grumbles22
Posted
Updated 14-Mar-11 11:52am
v2
Comments
Manfred Rudolf Bihy 14-Mar-11 17:53pm    
Edit: Added pre tags.

1 solution

I would suggest you write a small function which opens the file and puts the content into a variable. You can pass a file name to open to the function and the content will be returned in @CONTENTS

XML
sub open_file
{
    my $FILE = $_[0] ;
    open(FILE,"$FILE") or print ("unable to open $FILE file!", __LINE__);
        chomp ( @CONTENTS=<FILE>); close FILE;
}



Then you can read the contents from @CONTENTS line by line using foreach loop construct and do the processing with regex.

foreach $LINE ( @CONTENTS )
{
   //regex check and other modification necessary.
}


This should help you solve the problem.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900