Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2017-10-18 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (242 votes). Check out past polls.

    Notices?