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

x
?
Solved

Parsing LDAP search output

Posted on 2006-04-12
27
Medium Priority
?
760 Views
Last Modified: 2008-03-17
Hi experts,
 I have a script which does ldapsearch and ldapdelete for dn's entries with a specific attributes, but the ldapdelete does not work as the dn's are broken down into two lines for example
uid=idmtest,ou=people,ou=0007XXXXX,ou=Customer,ou=External,dc=portal,dc=asdas
,dc=net
instead of
uid=idmtest,ou=people,ou=0007XXXXX,ou=Customer,ou=External,dc=portal,dc=asdas,dc=net, as one single line

can anybody please help me with a perl code to parse my search outputs so that it prints in one sinlgle line.

Thanks
0
Comment
Question by:itsme_asif
  • 15
  • 11
27 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 16440583
Is your output saved as an array, each entry being one line, or 1 long string?

If they are multiple lines, use this to get it into 1 long string
#Join lines into 1 long string
my $outline=join("",@outarray);



Will a record always start with "uid=" and always end with "dc=net".  If so, use this to get records:
#For each record, print it on it's own line
while( $outline =~ m/^(uid=.*?dc=net)$/gsm ){
    #Now, $1 is 1 record, but it might contain newlines
   
    #Remove newlines
    my $line = $1;
    $line =~ s/\n//g;
   
    #Do whatever you want with a line here
    print "$line\n";
}



If the start is not always "uid=" or the end is not always "dc=net", what is used to start and end a record?
0
 

Author Comment

by:itsme_asif
ID: 16441164
This is the search ouput which i get it in a file
uid=ahsa12,ou=people,ou=000xxxxxx,ou=Customer,ou=External,dc=portal,dc=ss
ss,dc=net
ahsa12,people,000xxxxxx,Customer,External,dc=portal,dc=reyrey,dc=net

uid=treck92,ou=people,ou=000xxxxxx,ou=Customer,ou=External,dc=portal,dc=ss
ss,dc=net
treck92,people,000xxxxxx,Customer,External,dc=portal,dc=reyrey,dc=net

uid=mechanicus,ou=people,ou=000xxxxxx,ou=Customer,ou=External,dc=portal,dc=ss
ss,dc=net

and i want the parsing to be done like the following
uid=ahsa12,ou=people,ou=000xxxxxx,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net
ahsa12,people,000xxxxxx,Customer,External,dc=portal,dc=reyrey,dc=net

uid=treck92,ou=people,ou=000xxxxxx,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net
treck92,people,000xxxxxx,Customer,External,dc=portal,dc=reyrey,dc=net

uid=mechanicus,ou=people,ou=000xxxxxx,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net

so that the ldapdelete sees the full user dn in one line and delete is effective,
please help me a way out
0
 
LVL 85

Expert Comment

by:ozo
ID: 16441214
{local $/="\nuid=";
 while( <> ){
     s/\n(?!uid=|$)//g;
     print;
 }
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:itsme_asif
ID: 16441782
can i have the parse result to output in a file, because i have to do ldapdelete on the file.
0
 
LVL 85

Expert Comment

by:ozo
ID: 16441845
{local $/="\nuid="; local @ARGV=("filename");
 while( <> ){
     s/\n(?!uid=|$)//g;
     print;
 }
}
0
 
LVL 85

Expert Comment

by:ozo
ID: 16441864
#sorry, forgot $^I
 {local $/="\nuid="; local @ARGV=("filename"); local $^I=".bak":
 while( <> ){
     s/\n(?!uid=|$)//g;
     print;
 }
}
0
 

Author Comment

by:itsme_asif
ID: 16441981
Hi ozo,
 Is local @ARGV=("filename"); the input file name or the file output after parsing?
0
 
LVL 85

Expert Comment

by:ozo
ID: 16442021
Both.  (if $^I is also set)
0
 

Author Comment

by:itsme_asif
ID: 16442049
Hi ozo,
 This the script named parseldif.pl, i have based on what u had said

#!usr/bin/perl
{local $/="\nuid="; local @ARGV=("searchfile.ldif"); local $^I=".bak":
 while( <> ){
     s/\n(?!uid=|$)//g;
     print;
 }
}
 but when i execute ./parseldif.pl
i am getting a
ksh: parseldif.pl:  not found
I did made sure /usr/bin is in the path, can you please look into it?
0
 

Author Comment

by:itsme_asif
ID: 16442318
hi ozo,
i have the perl script working, but i get the following output
version: 1dn: uid=testasif203,ou=people,ou=0007688640,ou=Customer,ou=External,dc=portal, dc=reyrey,dc=netufn: testasif203,people,0007688640,Cu
stomer,External,portal,reyrey,net

but then i want
version: 1
dn: uid=testasif203,ou=people,ou=0007688640,ou=Customer,ou=External,dc=portal, dc=reyrey,dc=net

FYI: the version: 1 only comes in the first line of the ouput
0
 
LVL 85

Expert Comment

by:ozo
ID: 16442649
I thought all lines begin with "uid="
what was the input?
0
 

Author Comment

by:itsme_asif
ID: 16445258
sorry about the confusion

This is the output
version: 1
**********************************************************
dn: uid=BGILBERT,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=
 ssss,dc=net
ufn: BGILBERT,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=mickeymouse,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,
 dc=ssss,dc=net
ufn: mickeymouse,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=asif_test,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc
 =ssss,dc=net
ufn: asif_test,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=chtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=ss
 ss,dc=net
ufn: chtest,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=acmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=s
 sss,dc=net
ufn: acmtest,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=idmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=s
 sss,dc=net
ufn: idmtest,people,000XXXXX,Customer,External,portal,ssss,net
***********************************************************************

If you could see the dn's are broken into two lines, i want the dn in one single line , something like the following

dn: uid=idmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net




0
 

Author Comment

by:itsme_asif
ID: 16445592
sorry about the confusion

This is the output
**********************************************************
version: 1
dn: uid=BGILBERT,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=
 ssss,dc=net
ufn: BGILBERT,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=mickeymouse,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,
 dc=ssss,dc=net
ufn: mickeymouse,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=asif_test,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc
 =ssss,dc=net
ufn: asif_test,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=chtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=ss
 ss,dc=net
ufn: chtest,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=acmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=s
 sss,dc=net
ufn: acmtest,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=idmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=s
 sss,dc=net
ufn: idmtest,people,000XXXXX,Customer,External,portal,ssss,net
***********************************************************************

If you could see the dn's are broken into two lines, i want the dn in one single line , something like the following

dn: uid=idmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net

0
 
LVL 85

Expert Comment

by:ozo
ID: 16448302
Are only lines with that start with "dn:uid=" boroken, and are they always broken into two lines?  Are they always followed by a line starting with "ufn: " ?
0
 

Author Comment

by:itsme_asif
ID: 16449349
yeah thats right the following is exactly how the output is

version: 1
dn: uid=BGILBERT,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=
 ssss,dc=net
ufn: BGILBERT,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=mickeymouse,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,
 dc=ssss,dc=net
ufn: mickeymouse,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=asif_test,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc
 =ssss,dc=net
ufn: asif_test,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=chtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=ss
 ss,dc=net
ufn: chtest,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=acmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=s
 sss,dc=net
ufn: acmtest,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=idmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=s
 sss,dc=net
ufn: idmtest,people,000XXXXX,Customer,External,portal,ssss,net

Please not that i dont care about the entry 'ufn', all i want is to have the dn entry in one single line
0
 
LVL 85

Expert Comment

by:ozo
ID: 16449422
Assuming all "dn: " entries are broken into exactlty two lines, you can run this command from ksh
perl -i.bak -pe 'chomp if /^dn: /' searchfile.ldif
0
 

Author Comment

by:itsme_asif
ID: 16449572
let me try it and ill get back to you
0
 

Author Comment

by:itsme_asif
ID: 16450646
hi ozo,
although its making into one line ldapdelete command for some reason is breaking it into two lines and its not deletingg, can you please tell me a way of parsing in this following way
*************************************************
version: 1
dn:
uid=BGILBERT,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net
ufn: BGILBERT,people,000XXXXX,Customer,External,portal,ssss,net

dn:
uid=mickeymouse,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net
ufn: mickeymouse,people,000XXXXX,Customer,External,portal,ssss,net

dn:
uid=asif_test,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net
ufn: asif_test,people,000XXXXX,Customer,External,portal,ssss,net

dn:
uid=chtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net
ufn: chtest,people,000XXXXX,Customer,External,portal,ssss,net

dn:
uid=acmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net
ufn: acmtest,people,000XXXXX,Customer,External,portal,ssss,net

dn:
uid=idmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net
ufn: idmtest,people,000XXXXX,Customer,External,portal,ssss,net
*****************************************************************
0
 
LVL 85

Expert Comment

by:ozo
ID: 16450703
> although its making into one line ldapdelete command for some reason is breaking it into two lines and its not deletingg,
I'm no sure what you mean.
Can you post an example of what it is doing?
If you want "dn:" on a separate line, you might try
perl -i.bak -pe 'chomp if s/^dn:s+/dn:\n/' searchfile.ldif
0
 

Author Comment

by:itsme_asif
ID: 16452975
The above perl command is not doing anything Ozo,

Anyways this is the output after the perl command

perl -i.bak -pe 'chomp if /^dn: /' searchfile.ldif

version: 1
dn: uid=BGILBERT,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal,dc= ssss,dc=net
ufn: BGILBERT,people,000xxxxx,Customer,External,portal,ssss,net

dn: uid=mickeymouse,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal, dc=ssssy,dc=net
ufn: mickeymouse,people,000xxxxx,Customer,External,portal,ssss,net

dn: uid=asif_test,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal,dc =ssss,dc=net
ufn: asif_test,people,000xxxxx,Customer,External,portal,ssss,net

dn: uid=chtest,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net
ufn: chtest,people,000xxxxx,Customer,External,portal,ssss,net
**************************************
This is the result when i run the ldapdelete command

ldap_init( oh18u127.reyrey.com, 389 )
deleting entry version: 1
ldap_delete: No such object
deleting entry dn: uid=BGILBERT,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal,dc= reyrey,dc=net
ldap_delete: No such object
ldap_delete: matched: ou=people,ou=000xxxxx,ou=customer,ou=external,dc=portal,dc=ssss,dc=net
deleting entry ufn: BGILBERT,people,000xxxxx,Customer,External,portal,ssss,net
ldap_delete: No such object
deleting entry dn: uid=mickeymouse,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal, dc=ssss,dc=net
ldap_delete: No such object
ldap_delete: matched: ou=people,ou=000xxxxx,ou=customer,ou=external,dc=portal,dc=ssss,dc=net
deleting entry ufn: mickeymouse,people,000xxxxx,Customer,External,portal,ssss,net
ldap_delete: No such object
deleting entry dn: uid=asif_test,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal,dc =ssss,dc=net
ldap_delete: No such object
ldap_delete: matched: ou=people,ou=000xxxxx,ou=customer,ou=external,dc=portal,dc=ssss,dc=net
deleting entry ufn: asif_test,people,0000xxxxx,Customer,External,portal,ssss,net
ldap_delete: No such object

In the above result i dont care about the entry ufn:, but i feel if i could remove dn: and make
uid=mickeymouse,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal, dc=ssss,dc=net  , in one single line, ldapdelete might work, can you please help

0
 
LVL 85

Expert Comment

by:ozo
ID: 16459770
It looks like the output after the perl command that you show above is what you asked for.

I'm not sure what that has to do with the result when you run the ldapdelete command
which seems to have a lot of extra stuff that you didn't mention before, so I don't know what you want to do with that output.

Can you show what searchfile.ldif should look like before you run a perl command, and what you want it to look like after you run the perl command?
0
 

Author Comment

by:itsme_asif
ID: 16462016
This is the searchfile.ldif, contents
*************************************
version: 1
dn: uid=BGILBERT,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=
 ssss,dc=net
ufn: BGILBERT,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=mickeymouse,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,
 dc=ssss,dc=net
ufn: mickeymouse,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=asif_test,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc
 =ssss,dc=net
ufn: asif_test,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=chtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=ss
 ss,dc=net
ufn: chtest,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=acmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=s
 sss,dc=net
ufn: acmtest,people,000XXXXX,Customer,External,portal,ssss,net
************************************
dn: uid=idmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=s
 sss,dc=net
ufn: idmtest,people,000XXXXX,Customer,External,portal,ssss,net

and i want the ouput to be something like the following after the perl command.
*******************************
version: 1
dn:
uid=BGILBERT,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal,dc= ssss,dc=net
ufn: BGILBERT,people,000xxxxx,Customer,External,portal,ssss,net

dn:
uid=mickeymouse,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal, dc=ssssy,dc=net
ufn: mickeymouse,people,000xxxxx,Customer,External,portal,ssss,net

dn:
uid=asif_test,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal,dc =ssss,dc=net
ufn: asif_test,people,000xxxxx,Customer,External,portal,ssss,net

dn:
uid=chtest,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net
ufn: chtest,people,000xxxxx,Customer,External,portal,ssss,net
*****************************************************************

can you please help me out, please let me know if you would need some more clarification.

Thanks
0
 

Author Comment

by:itsme_asif
ID: 16462021
This is the searchfile.ldif, contents
*************************************
version: 1
dn: uid=BGILBERT,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=
 ssss,dc=net
ufn: BGILBERT,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=mickeymouse,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,
 dc=ssss,dc=net
ufn: mickeymouse,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=asif_test,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc
 =ssss,dc=net
ufn: asif_test,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=chtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=ss
 ss,dc=net
ufn: chtest,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=acmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=s
 sss,dc=net
ufn: acmtest,people,000XXXXX,Customer,External,portal,ssss,net

dn: uid=idmtest,ou=people,ou=000XXXXX,ou=Customer,ou=External,dc=portal,dc=s
 sss,dc=net
ufn: idmtest,people,000XXXXX,Customer,External,portal,ssss,net

and i want the ouput to be something like the following after the perl command.
*******************************
version: 1
dn:
uid=BGILBERT,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal,dc= ssss,dc=net
ufn: BGILBERT,people,000xxxxx,Customer,External,portal,ssss,net

dn:
uid=mickeymouse,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal, dc=ssssy,dc=net
ufn: mickeymouse,people,000xxxxx,Customer,External,portal,ssss,net

dn:
uid=asif_test,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal,dc =ssss,dc=net
ufn: asif_test,people,000xxxxx,Customer,External,portal,ssss,net

dn:
uid=chtest,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net
ufn: chtest,people,000xxxxx,Customer,External,portal,ssss,net
*****************************************************************

can you please help me out, please let me know if you would need some more clarification.

Thanks
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 16466746
perl -i.bak -pe 'chomp if s/^dn:s+/dn:\n/' searchfile.ldif
should do that
Are you saying it does not?
If not, are you sure the lines start with "dn: "
What does
oc -c searchfile.ldif show?
0
 

Author Comment

by:itsme_asif
ID: 16468780
Hi ozo,
 The ouput is unchanged when i execute the above command
perl -i.bak -pe 'chomp if s/^dn:s+/dn:\n/' searchfile.ldif

This is the ouput which is unchanged after the perl command
*******************************
version: 1
dn: uid=frenchtest7,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal,
 dc=ssss,dc=net
ufn: frenchtest7,people,000xxxxx,Customer,External,portal,ssss,net
*******************************************

and i want the ouput like the following
******************************
version: 1
dn:
uid=frenchtest7,ou=people,ou=000xxxxx,ou=Customer,ou=External,dc=portal,dc=ssss,dc=net
ufn: frenchtest7,people,000xxxxx,Customer,External,portal,ssss,net
************************************
0
 

Author Comment

by:itsme_asif
ID: 16477785
hi ozo, can you please update me on the perl command.
0
 
LVL 85

Expert Comment

by:ozo
ID: 16482275
The only reason I can think of that the output could be unchanged would be if /^dn:s+/ never matched, although it should have to look at the output.
Could you post the first few lines of
oc -c searchfile.ldif
maybe there's something else in the file that we're not seeing here.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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

581 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