Professional Web Applications Themes

How to process blank line delimeted blocks of text? - PERL Beginners

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

  1. #1

    Default How to process blank line delimeted blocks of text?

    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.

    Tim Guest

  2. #2

    Default Re: How to process blank line delimeted blocks of text?

    "Tim Musson" <net> wrote in message
    news:net... 

    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



    Paul Guest

  3. #3

    Default Re: How to process blank line delimeted blocks of text?

    Tim Musson wrote: 

    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
    Gunnar Guest

  4. #4

    Default Re: How to process blank line delimeted blocks of text?

    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.

    Tim Guest

Similar Threads

  1. Help: how with text-blocks?
    By A.C. in forum Macromedia Freehand
    Replies: 9
    Last Post: August 11th, 10:19 PM
  2. Text blocks whited out
    By Chris_Garaffa@adobeforums.com in forum Adobe Photoshop Mac CS, CS2 & CS3
    Replies: 5
    Last Post: April 7th, 09:13 PM
  3. FH MX bug with large text blocks?
    By MDD867 in forum Macromedia Freehand
    Replies: 0
    Last Post: July 24th, 06:34 AM
  4. Eliminating a blank line from a text field (MX)
    By Edward Wedig in forum Macromedia Flash
    Replies: 0
    Last Post: July 14th, 05:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139