in reply to How do I split a file into parts

There are several ways of accomplishing this task that I can think of. The one best one I can think of in terms of flexibility and efficiency is this (untested code).
my $fil_count = 0; my $delim = 'XXFFDDF'; open IN, 'in.txt' or die "Can't open in.txt: $!\n"; open OUT, '> out0.txt' or die "Can't write to out0.txt: $!\n"; while (<IN>) { if (/^(.*?)$delim(.*)$/) { print OUT $1 if $1; close OUT; $fil_count++; open OUT, '> out' . $fil_count . '.txt' or die "Can't write to out +${fil_count}.txt: $!\n"; print OUT $2 if $2; } else { print OUT $_; } } close IN;

Replies are listed 'Best First'.
Re: Answer: How do I split a file into parts
by merlyn (Sage) on Nov 23, 2000 at 22:38 UTC
    If you can stand putting the entire line with the delimiter in the new file, there's a quick and dirty version for this:
    perl -pe 'BEGIN { $x = "aaa000"; } open STDOUT, ">".(++$x) if /XXFFDDF +/' <input >initial_output

    -- Randal L. Schwartz, Perl hacker