Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Perl parse dump file

Posted on 2012-08-26
Medium Priority
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 81

Accepted Solution

arnold earned 2000 total points
ID: 38335987
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

ID: 38399449
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.


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans
Suggested Courses

580 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