I have written a script to split a large file into many small files. The large file is 1.8 gigabytes, or approximately 6.4 million lines. The objective is to break it into chunks so we can open the data and see what it contains because our machines cannot handle opening text files that large.

I have written a script that works but would like some feedback on better ways to accomplish the same thing. Maybe even faster ways.

use strict; use warnings; my $source = shift or &usage(); my $lines_per_file = shift or &usage(); open (my $FH, "<$source") or die "Could not open source file. $!"; open (my $OUT, ">00000000.log") or die "Could not open destination fil +e. $!"; my $i = 0; while (<$FH>) { print $OUT $_; $i++; if ($i % $lines_per_file == 0) { close($OUT); my $FHNEW = sprintf("%08d", $i); open ($OUT, ">${FHNEW}.log") or die "Could not open destinatio +n file. $!"; } } sub usage() { print <<EOF; PROGRAM NAME: Partition File DESCRIPTION: Takes a file and creates many small files out of the large file. EXAMPLE USAGE: partition_file.pl log.txt 1000 PARAMETERS: 1. Source File: File name of the source file to partition. 2. Maximum number of lines per file: The number of lines per file. EOF exit; }

Thanks fellow monks!
Best Regards,
disciple
thePfeiffers.net

In reply to Splitting Large File into many small ones. by disciple

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.