Assuming you don't mind that the file gets read several times over, this is the way I would do it. It ought to be fairly efficient.

  1. In a first pass, you must get an array of offsets of where lines start in the file. However, you don't need the offset for every single line.

    Divide the file into chunks of reasonable size. For example, if you allow at most about 500k from the file in memory at one, first seek in the file with a value close to 500_000, read one (incomplete) line, call tell on the handle, and store it in an array. Add 500_000 to that offset, and repeat, until you get past the file size. You end up with an array of indexes of where lines start. Note that the first value in the array must be 0; the last value ought to be the length of the file.

  2. Loop through that array backwards, each time getting one offset, and the next offset. seek to the lower value; read a block of as many bytes as the difference between the two. Reverse the lines in that block, and write it to a new file ,each time appending to what you wrote there last time.


In reply to Re: reversing files line by line by bart
in thread reversing files line by line by naturalsciences

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.