FAQ
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...

Or am I going about this all wrong? End result, I would like a report
of the members of the security groups daily and a diff of yesterdays
report from todays.

Thanks for any suggestions you may have.

btw, I am running this on MS, but if you have *nix examples, I can
work with that too.

Thanks again!

--
Tim Musson
Flying with The Bat! eMail v2.12.00
The days of the digital watch are numbered.

Search Discussions

  • Paul Lalli at Oct 11, 2004 at 7:42 pm
    "Tim Musson" <Tim@Musson.net> wrote in message
    news:456178807.20041011131450@Musson.net...
    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
  • Gunnar Hjalmarsson at Oct 11, 2004 at 7:52 pm

    Tim Musson wrote:
    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...

    Or am I going about this all wrong?
    How could we know, when you don't show us any code? Post a short but
    complete program that illustrates what you are currently doing.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
  • Tim Musson at Oct 13, 2004 at 1:13 pm
    Hey Luke,

    BL> Will this work?

    Yes, that did the trick, thanks!

    BL> {
    BL> local $/ = "\n\n";
    BL> while (<FILE>)
    BL> {
    BL> for $line (split /\n/, $_)
    BL> {
    BL> # process lines in a dn section here
    BL> next unless $line =~ /^(dn:|member:|groupType:)/;
    BL> }
    BL> }
    BL> }

    --
    Tim Musson
    Flying with The Bat! eMail v2.12.00
    The generation of random numbers is too important to be left to
    chance.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedOct 11, '04 at 5:14p
activeOct 13, '04 at 1:13p
posts4
users3
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase