Perl parse dump file

Posted on 2012-08-26
Last Modified: 2012-09-14
I have a file that has a dump of user records like an example below, each set of user records is separated by a \n (blank line)

I need to read the file in paragraph mode (usually use perl -n00e, but then check to see if each record contains a manager or managerfunctional attribute such as:

manager: CN=Juan Alcocer  494219,ou=employee,o=abc,c=AN
manager: JAlcocer
managerFunctional: JAlcocer

if the record contains a manager or managerfunctional I need to pull out the attributes of
(dn: id: alias: manager and managerfunctional)

I have a issue where some of the attributes for the manager or managerfunctional does not start with a CN=

Should be such as:
manager: CN=Juan Alcocer  494219,ou=employee,o=abc,c=AN
manager: JAlcocer  

What I really need to do is pull out only if a record contains a manager or managerfunctional that doe not have the CN= in it but if I can get all of them then I can parse that.

I'm using a one liner such as below and works for the most part but does not give me just what I want.

cat dump.ldif | perl -MMIME::Base64 -n00e 'BEGIN{@attrs=qw{ id alias admintext manager managerfunctional};print join(qq{\t},@attrs),qq{\n}};if(/^manager(|functional): [^c][^n][^=]/mi ){s/\n //g;foreach $a (@attrs){s/^($a:): *(.+)/$1.q{ }.decode_base64($2)/mieg;print q{"},join(qq{\n},m/^$a: (.+)/mig),qq{"\t}}print qq{\n}}'

Open in new window


Any suggestions how I can parse the file to get what I want from each record, need to get the attributes needed all one on line separated by a TAB so I can pull it up in an xls file.

Example of one of the records in the file, all records are separated by a blank line, the records a longer, I just removed some of the attributes to shorten the example, but still all the same, just has more attribuets in them.

Some of the records has multiple lines for the manager or managerfunctional which is what is giving me problems, I don't get all the lines when there is multiple manager or managerfunctional lines.

Example of one of the records in the dump file:

dn: CN=Jeffrey Doe  492919,ou=employee,o=SLB,c=AN
modifyTimestamp: 20120727165756Z
modifiersName: cn=ldap admin 100000,ou=role,o=slb,c=an
adminText: QMM:ADmigrated 2012-4-11
lastModifiedBy: using special rights
lastModifiedOn: 20120727
manager: CN=Juan Alcocer  494219,ou=employee,o=SLB,c=AN
manager: JAlcocer
securityStatus: Training:Level2:20110804
securityStatus: Training:Level1:20110804
securityStatus: QandA:20110916
securityStatus: Password:20120514:good
managerFunctional: JAlcocer
emergencyContactsSaved: Declined
emergencyContactsSaved: 20120322
street: 6213 W County Rd 112
ou: Shared Servicest
languages: Englishns
jobCategory: Engineering
jobCategory: Operations (Field)
c: US
givenNameLegal: Jeffrey07
objectClass: inetOrgPerson
objectClass: slbAdmin
objectClass: slbOrgPerson
objectClass: slbHomePerson
objectClass: organizationalPerson
objectClass: top
objectClass: person
ID: 492919
alias: JDoe
cn: Jeffrey Doe
cn: Jeffrey Doe  492919
displayName: Jeffrey Doe
employeeID: O998807
employeeType: employeen
createTimestamp: 20100826084448Z

cat fulldump.ldif | perl -MMIME::Base64 -n00e 'BEGIN{@attrs=qw{ id alias admintext manager managerfunctional};print join(qq{\t},@attrs),qq{\n}};if(/^manager(|functional): [^c][^n][^=]/mi ){s/\n //g;foreach $a (@attrs){s/^($a:): *(.+)/$1.q{ }.decode_base64($2)/mieg;print q{"},join(qq{\n},m/^$a: (.+)/mig),qq{"\t}}print qq{\n}}'

Open in new window

Question by:bt707
    LVL 76

    Accepted Solution

    Did not look closely enough to interpret what your command line does.

    You could use a line by line check for the match of interest, until an empty line

    If ( /manager:\s+(CN.*)$/)  {$managercn=$1;$matched=1;}
    $1 will have the CN string when matched.
    You can use a test for matched or simply look for the other items of interest if they do not follow a specific order, when a new line/record separator is detected check whether $matched==1 and at that point print out the variables of interest while resetting matched back to 0

    You could use an append to variables that have multiple entries
    Not sufficiently familiar with your attribute construct to now whether you can create a rule that will append when multiple occurrences of an attribute occur.

    Author Closing Comment

    Thanks for the tip, that did not help for what I was doing but did help and gave me some ideas and I got it working.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now