You can use Tac (Unix) if you are on a Unix like operating system. It's much faster than using Perl and File::ReadBackwards:
# ls -lh /var/lib/mysql/ibdata1 -rw-rw---- 1 mysql mysql 66M Mar 19 11:05 /var/lib/mysql/ibdata1 # wc -l /var/lib/mysql/ibdata1 323833 /var/lib/mysql/ibdata1 # time tac /var/lib/mysql/ibdata1 > /tmp/bar real 0m0.643s user 0m0.173s sys 0m0.322s # time /usr/bin/perl > /tmp/foo use warnings; use strict; use File::ReadBackwards; my $bw = File::ReadBackwards->new( '/var/lib/mysql/ibdata1' ) or die "$!" ; while( defined( my $log_line = $bw->readline ) ) { print $log_line ; } __END__ real 0m29.076s user 0m11.910s sys 0m10.819s # md5sum /tmp/foo 2b31d9f47525853842d5dbce584bd95c /tmp/foo # md5sum /tmp/bar 2b31d9f47525853842d5dbce584bd95c /tmp/bar
Update Wed Mar 26 10:05:03 CET 2008: Added number of lines in input file.

Update Wed Mar 26 10:11:31 CET 2008: It seems that the tac + process file forwards using Perl approach combined is still much faster than process file using Perl and File::ReadBackwards:

# time /usr/bin/perl > /tmp/foo use warnings; use strict; use File::ReadBackwards; my $bw = File::ReadBackwards->new( '/var/lib/mysql/ibdata1' ) or die "$!" ; while( defined( my $log_line = $bw->readline ) ) { $log_line =~ s/1/2/g; print $log_line; } __END__ real 0m27.431s user 0m12.906s sys 0m10.701s [root@afflinux aff]# time /usr/bin/perl > /tmp/bar use warnings; use strict; my $FH = undef; open($FH, '/var/lib/mysql/ibdata1' ) or die "$!" ; while( my $log_line = <$FH> ) { $log_line =~ s/1/2/g; print $log_line; } __END__ real 0m5.249s user 0m1.535s sys 0m0.293s
--
Andreas

In reply to Re: How to reverse a huge file in Perl? by andreas1234567
in thread How to reverse a huge file in Perl? by MelaOS

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.