Hello JediWombat,

Perl may call bzcat directly via open, similarly to calling bzcat from shell.

See tip by kcott for blank input field separator. Thanks kcott for the tip.
See tip by Anonymous Monk for reading from bzcat directly.

#!/usr/bin/perl use strict; use warnings; my $ldif = "file.ldif.bz2"; my $match = "g2ucab"; $/ = ""; open my $fh, "-|", "/usr/bin/bzcat $ldif" or die "open error ($ldif): $!"; while ( <$fh> ) { if ( /uid=$match/m ) { print $_; last; } } close $fh;

Parallel processing is another possibility when involving extra work inside the block, which isn't the case here. Thus, the next demonstration will not run any faster. Workers read 8 blocks at a time, configurable via the chunk_size option. Calling $mce->last causes all workers to leave the parallel block, similarly to calling last inside a while loop.

The reason that this small use-case doesn't run any faster is due to workers waiting on bzcat.

#!/usr/bin/perl use strict; use warnings; use MCE::Loop; my $ldif = "file.ldif.bz2"; my $match = "g2ucab"; $/ = ""; open my $fh, "-|", "/usr/bin/bzcat $ldif" or die "open error ($ldif): $!"; MCE::Loop->init( max_workers => 3, chunk_size => 8, use_slurpio => 1, ); mce_loop { my ( $mce, $slurp_ref, $chunk_id ) = @_; open my $local_fh, "<", $slurp_ref; while ( <$local_fh> ) { if ( /uid=$match/m ) { print $_; $mce->last; } } close $local_fh; } $fh; MCE::Loop->finish; close $fh;

Regards, Mario


In reply to Re: Searching large files a block at a time by marioroy
in thread Searching large files a block at a time by JediWombat

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.