dhaval0203 has asked for the wisdom of the Perl Monks concerning the following question:

hi please tell me how to run the following script which is about to spit a file in to number of parts, and how to give parts number i.e. if i want to split a file in 10 parts then how to do it? please reply me fast.......

#!/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;

Replies are listed 'Best First'.
Re: how to run script?
by almut (Canon) on May 12, 2010 at 07:57 UTC
    please tell me how to run the following script

    Those two lines

    my $parts = shift; ### how many parts to split my @file = @ARGV; ### the files to split

    suggest that the first argument is the number of desired parts, and any remaining arguments are the names of the files to split, i.e., for example

    ./yourscript.pl 10 file_to_split

    or maybe (if your OS doesn't support shebang lines (#!/usr/bin/perl), and you have no association set up):

    perl yourscript.pl 10 file_to_split