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

hi monks, Can anybody tell me is their any possible to split the 8 GB file as 512 MB. If their is an possible function for that in perl could you please instruct me so.

Replies are listed 'Best First'.
Re: splitting a file
by Samy_rio (Vicar) on Jul 05, 2007 at 06:02 UTC

    Hi boby, this file split works under Windows,

    use strict; use warnings; use File::Split; my $fs = File::Split->new({keepSource=>'1'}); my $filename = 'E:\test\text.txt'; my $size = (stat($filename))[7]/1024/1024; #####Based on Number of Files my $files_out = $fs->split_file({'parts' => sprintf("%.0f",($size/512) +)}, $filename);

    First look into How do I post a question effectively? and How (Not) To Ask A Question

    ikegami++

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: splitting a file
by ikegami (Patriarch) on Jul 05, 2007 at 05:44 UTC

    Sounds like you want the unix tool split.

    It's pretty simple to do in Perl too. What have you tried?

Re: splitting a file
by hangon (Deacon) on Jul 05, 2007 at 05:57 UTC

    I don't see why not. Following is one possible way. This should break up your 8 GB file into 512 MB files 1 MB at a time. It's untested as I don't have an 8 GB file handy, but should give you the idea.

    my $filename = 'foo'; my $bufferSize = 1,000,000; my $outFileSize = 512; open (IN, $filename); binmode IN; seek (IN, 0, 0); my $buffer; my $i = 1; my $j = 1; open (OUT, "$filename$i"); while (read (IN, $buffer, $bufferSize)){ if ($j > $outFileSize){ $j = 1; $i++; close OUT; open (OUT, "$filename$i"); } print OUT $buffer; $j++; } close OUT;

    UPDATE: No I don't manufacture hard drives, so as Fletch correctly points out, $bufferSize should be 1048567 bytes. Oh, and leave out the commas.

      $ perl -le '$oh_really = 1,000,000; print "$oh_really?"' 1?

      What you actually meant was 1_000_000, but if you really wanted 1MB that's 1_048_576 (or 1024 * 1024). Unless of course you're a hard drive manufacturer . . .</pedant>