"Tim Musson" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> I keep trying to do this for different things. I have not found a
> method I like yet.  So here is specifically what I am trying to do
> *this time*.
>
> My current customer has been having problems with members dropping out
> of various MS Active Directory Security Groups. So I figured I would
> toss a bit of code together in my spare time (yah, like that exists).
>
> I have the bit that dumps an LDIF file starting at a particular point
> in the AD Tree. Where I am hitting a wall is processing that LDIF
> file...
>
> Here is an example of my data (note the blank line separating dn:'s.
> ,-----
> | dn: CN=p106730,OU=Scr-Prt,OU=AB,DC=Stuff
> | ====8<---------------- snip
> | member: CN=trn,OU=Scripts,OU=AB,DC=Stuff
> | member: CN=uci,OU=Scripts,OU=AB,DC=Stuff
> | ====8<---------------- snip
> | groupType: -2147483644
> | ====8<---------------- snip
> | whenChanged: 20040927234946.0Z
> | whenCreated: 20030401203303.0Z
> |                                               <<<< Blank Line Here
> | dn: CN=p101714_mb8,OU=Scr-Prt,OU=AB,DC=Stuff
> | ====8<---------------- snip
> | groupType: -2147483644
> | ====8<---------------- snip
> | whenChanged: 20040927234926.0Z
> | whenCreated: 20020531152229.0Z
> `-----
>
> This is what I want to keep for each (blank line separated) dn:.
> ,----- [  ]
> | dn: CN=p106730,OU=Scr-Prt,OU=AB,DC=Stuff
> | member: CN=trn,OU=Scripts,OU=AB,DC=Stuff
> | member: CN=uci,OU=Scripts,OU=AB,DC=Stuff
> | groupType: -2147483644
> |
> | dn: CN=p101714_mb8,OU=Scr-Prt,OU=AB,DC=Stuff
> | groupType: -2147483644
> `-----
>
> I know I can change the record separator to \n\n to get one dn:
> section at a time, but how do I then process the individual lines? I
> don't seem to ever have any luck setting the record separator back to
> \n...

I don't see how resetting $/ back to \n would help you once you've read
and stored the data.  If you have a list of records, why can't you just
operate on that list?

#!/usr/bin/perl
use strict;
use warnings;

my @records;
{
    #read the file, one 'paragraph' at a time.
    local $/ = "\n\n";
    open my $file, "file.txt" or die "Cannot open file: $!";
    @records = <$file>;
}

#for each record, split on the newline, and keep only the lines you want
#then reconnect the lines into one record
foreach my $record (@records){
    my @data = grep /^(dn|member|groupType):/, (split /\n/, $record);
    $record = join "\n", @data;
}

#print the records
print join "\n\n", @records;
__END__

Note that there are several places where this could be shortened, but I
decided against playing golf this time around. :-)

Hope this helps,
Paul Lalli




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to