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

Dear Master

I need to split a file by sizewise. e.g. I have a file size 10 MB, that file I have to split into 2 MB. Every File should have size 2MB.

Please guide me.

Thanks you

Replies are listed 'Best First'.
Re: Split a file
by gopalr (Priest) on Aug 05, 2005 at 08:34 UTC
Re: Split a file
by Corion (Patriarch) on Aug 05, 2005 at 08:39 UTC

    Hello and Welcome to the Guided Quick Tour to the Perl Documentation. Today, we will focus on two important aspects of Perl variables that will be of convenient assistance when needing to split files.

    To your left, you find perlvar, also accessible as perldoc perlvar when using the command prompt. Please take a close look at the "input record separator", and especially how setting it to a reference to a number will make Perl read in records of a fixed length.

    To your right, you will find binmode, also accessible as perldoc -f binmode from your command prompt. Using binmode on any filehandle has become almost mandatory on any operating system when you want to prevent any character set manipulation that Perl might perform and when you want to emancipate yourself from whatever character set your operating system believes to be current.

    If you use these two features in conjunction, for example, in a loop, you can easily emulate the Unix split command, which does the same thing.

    This concludes our Guided Quick Tour to the Perl Documentation, if you have further question, please read the leaflet How (Not) To Ask A Question before continuing. If you haven't received the leaflet at the start of the tour, there is an online version available as well.

      And let us not forget the classic sysread as another way of reading a chunk of raw data of a fixed size.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Split a file
by gellyfish (Monsignor) on Aug 05, 2005 at 08:40 UTC

    As a completely non-Perl solution - if you are on a Unix-Like operating system you can use split which is designed to do just what you want.

    /J\

Re: Split a file
by BrowserUk (Patriarch) on Aug 05, 2005 at 09:26 UTC

    As a one-liner (wrapped for posting)

    Adjust the 2**21 (2MB) to suit. The output files are given the input filename.n (n=0 .. n)

    perl -e"BEGIN{$/=\2**21;binmode STDIN; $n=0}" -ne"open my $fh,'>:raw', $ARGV.'.'.$n++; print $fh $_;" filename

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: Split a file
by newroz (Monk) on Aug 05, 2005 at 09:03 UTC

    Cast that at shell
    split --bytes=2m input output
    it will produce output(s) with letters suffixes