Loading ...
Sorry, an error occurred while loading the content.
 

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

Expand Messages
  • david_bastian
    I copied your script and sample data and used them exactly as you had written them without making any changes. Your script works exactly as it has been told
    Message 1 of 3 , Feb 3, 2005
      I copied your script and sample data and used them exactly as you
      had written them without making any changes. Your script works
      exactly as it has been told to work. Your additional data files are
      on your system, but you will have to look for them in a different
      place.

      Here's why... On line #9 you are reading your input file from your
      D: drive. On line #11 you are saving the first matched occurance to
      your D: drive. However, on line #26 you are saving each successive
      match to a file on your C: drive.

      Look on your C: drive and you'll find your "missing" data files!

      Hope this helps...

      David

      --- In beginning_perl@yahoogroups.com, "mcolav" <mcolav@y...> wrote:
      >
      >
      > 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, 'D:\test.txt' ) )
      > {
      > $filename = sprintf( 'D:\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 );
      > }
    Your message has been successfully submitted and would be delivered to recipients shortly.