If I am understanding your question correctly, you want to split a 3GB text file into 200MB parts, but some lines are being split between files.

The functions read() and sysread() operate in characters and don't have a concept of lines. So, if you read exactly 200MB of data, there is a high probability that the boundary will be in the middle of a line.

One way to do it would be to read in lines, instead of characters, from the big file and print them to a new file part, keeping track of the number of characters read so far. When the character count goes over 200MB, you close the current file part and open the next one. Here is an example:

#!perl # Untested use 5.012; my $partsize = 200 * 1024 * 1024; my $file = shift or die 'no file'; open my $in, '<', $file or die "Can't open '$file' for reading: $!"; my $part = 1; my $size = 0; open my $out, '>', "$file.part$part" or die "Can't open '$file.part$part' for writing: $!"; while (<$in>) { print $out $_; $size += length $_; if ( $size >= $partsize ) { close $out; $part++; open $out, '>', "$file.part$part" or die "Can't open '$file.part$part' for writing: $!"; $size = 0; } }

In reply to Re: Large file split into smaller using sysread() by kejohm
in thread Large file split into smaller using sysread() by rkshyam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.