Another way of doing it is going through your big file and filling an array of (say) 1000 lines (or as many as you would like to keep) replacing the oldest lines by newer lines in a round-robin-type of fashion. At the end of the big file you have your 1000 lines in the array.

If you don't know beforehand how long the lines are, I think it is the best and fastest way of doing it (you don't have to read in your big file twice as you did), with the exception perhaps of the modules already suggested.

Update: As a matter of fact this is the way the Perl Power Tools tail-function works also (but it has a lot more options, which you perhaps do not need) and hereafter follows the relevant code from the Power Tools (slightly adapted):

while(<$fh>) { $i++; $buf[$i%($p)] = $_; } my @tail = (@buf[ ($i%($p) + 1) .. $#buf ], @buf[ 0 .. $i%($p)]); for (@tail) { print if $_; }
$fh is the filehandle to your big file and $p is the number of lines you need

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law


In reply to Re: breaking up very large text files - windows by CountZero
in thread breaking up very large text files - windows by antichef

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.