Parsing LDAP search output

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
itsme_asifAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Adam314Commented:
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
itsme_asifAuthor Commented:
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
ozoCommented:
{local $/="\nuid=";
 while( <> ){
     s/\n(?!uid=|$)//g;
     print;
 }
}
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

itsme_asifAuthor Commented:
can i have the parse result to output in a file, because i have to do ldapdelete on the file.
0
ozoCommented:
{local $/="\nuid="; local @ARGV=("filename");
 while( <> ){
     s/\n(?!uid=|$)//g;
     print;
 }
}
0
ozoCommented:
#sorry, forgot $^I
 {local $/="\nuid="; local @ARGV=("filename"); local $^I=".bak":
 while( <> ){
     s/\n(?!uid=|$)//g;
     print;
 }
}
0
itsme_asifAuthor Commented:
Hi ozo,
 Is local @ARGV=("filename"); the input file name or the file output after parsing?
0
ozoCommented:
Both.  (if $^I is also set)
0
itsme_asifAuthor Commented:
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
itsme_asifAuthor Commented:
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
ozoCommented:
I thought all lines begin with "uid="
what was the input?
0
itsme_asifAuthor Commented:
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
itsme_asifAuthor Commented:
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
ozoCommented:
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
itsme_asifAuthor Commented:
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
ozoCommented:
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
itsme_asifAuthor Commented:
let me try it and ill get back to you
0
itsme_asifAuthor Commented:
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
ozoCommented:
> 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
itsme_asifAuthor Commented:
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
ozoCommented:
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
itsme_asifAuthor Commented:
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
itsme_asifAuthor Commented:
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
ozoCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itsme_asifAuthor Commented:
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
itsme_asifAuthor Commented:
hi ozo, can you please update me on the perl command.
0
ozoCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.