Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Answer: How do I split a file into parts

by chipmunk (Parson)
on Nov 28, 2000 at 01:48 UTC ( #43551=categorized answer: print w/replies, xml ) Need Help??

Q&A > files > How do I split a file into parts - Answer contributed by chipmunk

This is probably the simplest approach:
$/ = 'XXFFDDF'; # set the input record separator my $base = 'filename'; my $i = 0; while (<>) { # read one section at a time my $filename = "$base$i"; # generate a new filename open(OUT, ">$filename") # create and write a new file or die "Can't open $filename: $!\n"; print OUT; $i++; }

You can generate the filenames however you prefer. I just chose a very simple way of generating the names as an example.

If each section is very long, you might want to read the file in in smaller chunks to conserve memory.

One final note: this puts the XXFFDDF at the end of each file. If you want to put it at the beginning, the code will need to be somewhat different.

Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2018-07-22 05:11 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (451 votes). Check out past polls.