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

Folks I need your help because I have several 128 MB binary files that I need to split into 10MB pieces and then join them later, but I'm not sure if split can do this with binary files?

TIA for helping me to solve my ignorance

Replies are listed 'Best First'.
Re: Split a binary file in pieces
by almut (Canon) on Mar 09, 2010 at 19:55 UTC
    #!/usr/bin/perl $/ = \10_000_000; # according to IEEE, otherwise \10_485_760 open my $in, "<", $ARGV[0] or die $!; binmode $in; my $n = 1; while (<$in>) { open my $out, ">", "outfile_".$n++ or die $!; binmode $out; print $out $_; }

    Update: changed <> to a manually opened <$in>, because I couldn't find a sensible way to binmode ARGV...  Does anyone know how to do it?

    Update-2:  use open IN => ':raw'; does seem to work for <>.  Or something ugly as

    while (<>) { binmode(ARGV), seek(ARGV,0,0), next if $.==1; ...
      I'm learning Perl. Thanks for the code. But how do we join the split pieces together? Thanks :)
        Write out how you would do it in words, then translate words into code
Re: Split a binary file in pieces
by Corion (Patriarch) on Mar 09, 2010 at 19:39 UTC

    See the split manpage, or if you want to do that with Perl, see perlvar on $/ and binmode.

    Update: Used a proper link to the split manpage

      This being perlmonks, [man://split] => split
Re: Split a binary file in pieces
by lupey (Monk) on Mar 10, 2010 at 14:01 UTC
    If you are using *nix, I'd let the shell handle this through the use of split
    to break apart the file and cat to concatenate the resulting files together.