Hi all,
I'm fairly new to Perl, but I'm in a job where a lot of existing Perl code is in use, so I'm working off what I have available and might be getting in over my head. I'm hoping you can help.
I have a large LDIF file in bzip format that I want to search, and for the block (as defined by blank lines before and after it) that matches my pattern, print the block.
Looking at a shell script we have here, which pipes into perl:
/usr/bin/bzcat $ldif | perl -e "$/ = \"\n\n\";
while (<>) {
if (/uid=$match/) {
print $_ ;
last;
} ;
}"
This uses the $/ input field separator, and then uses while (<>) to read a block at a time. I'd like to do this in pure perl, but I can't find a way. I'm using the IO::Uncompress::Bunzip2 module, which gives me an IO:File object, but the only way I can seem to interact with this in a useful way is with getline() or getlines() - neither of which let me iterate through the file a block at a time, like the shell script does.
Like I said, I am pretty new to Perl, so I'm sure there's a better way to do this. Can someone offer some assistance please? Here's the code I've got, which works, but is very slow:
my $z = new IO::Uncompress::Bunzip2 $file;
$mbnum = $ARGV[0];
while ($line = $z->getline()) {
if ($line =~ /^dn: uid=$mbnum,/) {
$found = "true";
print $line;
for (my $i=0; $i<100; $i++) {
$matchLine = $z->getline();
print "$matchLine";
if ($matchLine =~ /^$/) {
last;
}
}
}
if ($found eq "true") {
last;
}
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.