For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2005 > Splitting large file into little chunks based on tagging (example & script inc.)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Splitting large file into little chunks based on tagging (example & script inc.)
FlashMX

2005-01-25, 3:56 am

Hi,

I have a large file that I want to split into smaller chunks based on
a start and end text (<start>...<end> )

My script works only partially. It only saves the first occurence of
my match and then closes the script. How can I get it to keep ripping
throught the file saving into individual files.

My file I'm reading in is called test.txt

Below is just an example.

blah...blah...
blah...blah...
blah...blah...

<start>
....
<end>

blah...blah...
blah...blah...
blah...blah...

<start>
....
<end>

and so on...


Here is my script:

#!/usr/bin/perl -w

use strict;

my $written = 0;
my $index = 1;
my $filename;

if ( open( FH, 'C:\test.txt' ) )
{
$filename = sprintf( 'C:\out%d.txt', $index );
open( OUT, ">$filename" );
while ( <FH> )
{
if ( m|^<start>$| ... m|^<end>$| )
{
print OUT $_;
$written = 1;
}
else
{
if ( $written )
{
close( OUT );
$index++;
$filename = sprintf( 'C:\out%d.txt', $index );
open( OUT, ">$filename" );
$written = 0;
}
}

print "-> $_";

}
close( FH );
}


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com