I have a need to reverse the order of text lines in a HUGE, text file so I cant just @ filehandle and then reverse. I was going for a solution to read file in line by line then to write those lines out in a way that they will be appended to the beginning of a file. code i smashed together from bits and peaces from the web looked like this
#!/usr/bin/perl -w use strict; use warnings; my $file = 'hugefiletobereversed'; open FH, 'hugefiletobereversed'; while (our $newline = <FH>) { { local @ARGV = ($file); local $^I = '.bac'; while(<>){ if ($. == 1) { print "$newline\n"; } else { print; } } } }
Reaction is that it will only take the last line and print it in front of the file and then generate a lots of empty lines. Can anybody help me with this code or suggest a better one. JP Updated. Thanks for your replys. I found a module File::ReadBackwards that soothed all my problems in a sec :D Though im still curious whats the problem with my original code. And no it is not somekind of a programming homework - I have to work with several bioinformatics programs for my thesis, and being academic software they are usually not at all meant for compability. So i have to reformat outputfiles from some of them to use as input files for others. Sometimes the formatting differences are small and easy, sometimes differences are large but solutions are easy and this time the difference was small ( reverse the file) but solution just dindt came to my mind :D Also files tend to be in several Gb-s so any solutions involving reading those files in memory are out. ( makes my life harder)

In reply to 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.