Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: How do i split a file into given number of parts.

by mr_mischief (Monsignor)
on Jun 23, 2001 at 01:29 UTC ( [id://90875]=note: print w/replies, xml ) Need Help??


in reply to How do i split a file into given number of parts.

This solution takes as its first argument the number of ways to split a file, and the remainder of arguments are taken to be files which need to be split. It does not delete the originals.

#!/usr/bin/perl -w use strict; ### a must my $parts = shift; ### how many parts to split my @file = @ARGV; ### the files to split foreach ( @file ) { ### how big should the new file be? my $size = -s $_; my $buf_size = $size / $parts; ### ready a buffer for the data my $buf = ''; ### open the input file open ( In, $_ ) || warn "Cannot read $_: $!\n"; binmode ( In ); ### for as many parts as there are, read ### the amount of data, then write it to ### the appropriate output file. for ( my $i = 0; $i < $parts; $i++ ) { ### read an output file worth of data read ( In, $buf, $buf_size ) || warn "Read zero bytes from $_!\n"; ### write the output file open ( Out, "> $_$i" ) || warn "Cannot write to $_$i: $!\n"; print Out $buf; ### if this is the last segment, ### grab the remainder if ( $i == ( $parts - 1 ) ) { my $rem = $size % $buf_size; if( $rem ) { read ( In, $buf, $rem ) || warn "Read zero bytes from $_!\n"; print Out $buf; } } ### we are done with the current output file close ( Out ); } ### we're done spliting the input file close ( In ); } exit;


Its weakness is it only loads one buffer per output segment, and for greater usability it should probably loop if it's a huge size. I don't see this being a huge problem given the nature of the program, though, because there's some place the split files are going that's too small for the original file, so the parts should be small enough for main memory. I'll therefore leave more exotic buffer manipulation as an exercise.

Chris
Boo!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://90875]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found