in reply to Re^4: Sort big text file - byte offset - 50% there (Added code)
in thread Sort big text file - byte offset - 50% there

For some reason, it throws a warning while printing the sorted log. According to the output of warn, its always on the last line of the unsorted log.

It's generally a good idea to include a cut&paste of the warning message!

I don't see any potential for using vec to save memory here.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re^5: Sort big text file - byte offset - 50% there (Added code)

Replies are listed 'Best First'.
Re^6: Sort big text file - byte offset - 50% there (Added code)
by msalerno (Beadle) on Aug 14, 2006 at 16:03 UTC
    Here is the output of the script including the warning. Good point, sorry
    Begin Index
    Indexed 1167064 Lines in 00 Days, 00 Hours, 00 Minutes, 20 Seconds
    Begin Sort
    Sorted 1167064 Lines in 00 Days, 00 Hours, 00 Minutes, 00 Seconds
    1132616958:393264867 at D:/Logs/biglog.unsorted.log line 32, <BIGLOG> line 1168107.
    Total runtime:          00 Days, 00 Hours, 00 Minutes, 47 Seconds
    

      I think the problem is in your indexing loop (and goes right back to your OP).

      my @index; while (<BIGLOG>){ my $offset = tell BIGLOG; ### This offset is the start of the *nex +t* line! my $epoch = ( /^\s*#/ or /^\s\n/ or $_ !~ /^\s*\d/ ) ? 0 : Mktime( unpack 'A4xA2xA2xA2xA2xA2', $_ ); push @index, pack 'NN', $epoch, $offset; }

      You read a line, record the file position, and then pair that file position with the epoch info from the line you read. But that offset is the start of the next line, not the one you just read. The result is that all the offsets are one line displaced, so that when you come to try and read, having seek'd to the last offset (which is end of file), there is nothing left to read, so it fails.

      You need to recast that loop something like this:

      my( $offset, @index ) = 0; ## The first lines offset is zero while (<BIGLOG>){ my $epoch = ( /^\s*#/ or /^\s\n/ or $_ !~ /^\s*\d/ ) ? 0 : Mktime( unpack 'A4xA2xA2xA2xA2xA2', $_ ); push @index, pack 'NN', $epoch, $offset; ## Pair with previous off +set $offset = tell BIGLOG; ## and now get the start of the next line + }

      You probably should be checking the return code from seek also.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Thank you very much. I was so focused on getting the pack and sort working correctly that I completely overlooked that loop. For anyone who might be interested in doing something similar in the future, here is a stripped down version that works.
        Once again, thanks everyone.
        #!/usr/bin/perl -w use strict; use Date::Calc qw(Mktime); open (BIGLOG, "< D:/Logs/biglog.unsorted.log") || die "Cannot open unsorted log $!"; my( $offset, @index ) = 0; while (<BIGLOG>){ my $epoch = ( /^\s*#/ or /^\s\n/ or $_ !~ /^\s*\d/ ) ? 0 : Mktime( unpack 'A4xA2xA2xA2xA2xA2', $_ ); push @index, pack 'NN', $epoch, $offset; $offset = tell BIGLOG; } @index = sort {$a cmp $b} @index; open (OUTFILE, "> D:/Logs/biglog.sorted.log") || die "Cannot write sor +ted log $!"; while (@index){ print OUTFILE readline_n(\*BIGLOG, shift @index); } close BIGLOG; close OUTFILE; exit; sub readline_n{ my( $fh, $line) = @_; seek ($fh, unpack( 'N',substr( $line, 4, 4 )), 0) || warn "Problem + seeking to $line $!\n"; scalar <$fh> }