Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

fixing broken ldif files (replacing = with : )

by acser (Novice)
on Dec 09, 2004 at 04:34 UTC ( [id://413421]=perlquestion: print w/replies, xml ) Need Help??

acser has asked for the wisdom of the Perl Monks concerning the following question:

All, I need to convert LDIF files that were generated by a broken ldapsearch utility. Below is are the requested formats: From broken LDIF:
uid=HAMonitor1,ou=People,o=test.com userPassword={SHA}[deleted] uid=HAMonitor1 givenName=HA objectClass=top objectClass=person objectClass=organizationalPerson objectClass=inetorgperson sn=Monitor cn=HA Monitor uid=HAMonitor2,ou=People,o=test.com uid=HAMonitor2 givenName=HA objectClass=top objectClass=person objectClass=organizationalPerson objectClass=inetorgperson sn=Monitor cn=HA Monitor userPassword={SHA} [deleted]
To good LDIF:
dn: uid=HAMonitor1,ou=People,o=test.com userPassword: {SHA}[deleted] uid: HAMonitor1 givenName: HA objectClass: top objectClass: person objectClass: organizationalPerson objectClass:inetorgperson sn: Monitor cn: HA Monitor dn: uid=HAMonitor2,ou=People,o=test.com uid: HAMonitor2 givenName: HA objectClass: top objectClass: person objectClass: organizationalPerson objectClass: inetorgperson sn: Monitor cn: HA Monitor userPassword: {SHA} [deleted]
Here's my initial code that works but is very ugly. Can you please review, comment and perhaps suggest a more elegant approach. Thanks, Andras
#!/usr/bin/perl $prev_line = "\n"; while (<>) { chop; $this = $_; # print "DEBUG: original line: $this\n"; if ($prev_line =~ m/^\s+$/) { # don't do anything # print "DEBUG: empty line\n"; $this = "dn: $this"; } else { # replace first token = to first token: # print "DEBUG: full line\n"; ($firsttoken, $remainder) = split (/=/, $this, 2); $this="$firsttoken: $remainder"; } $prev_line = $_; if ($_ =~ m/^$/ ) { print "$_\n"; $prev_line = "\n"; } else { print "$this\n"; } }

Replies are listed 'Best First'.
Re: fixing broken ldif files (replacing = with : )
by BrowserUk (Patriarch) on Dec 09, 2004 at 04:55 UTC

    Try this

    perl -000 -pe"s[(^\w+)=][$1: ]mg" ldif.bad >ldif.good

    Examine what is said, not who speaks.
    "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      Or an even simpler solution, not using perl:

      sed -e 's/=/: /' ldif.bad > ldif.good
Re: fixing broken ldif files (replacing = with : )
by strat (Canon) on Dec 09, 2004 at 08:43 UTC
    Well, I don't know how is the exact rule for the dn, but I'd do it about the following way:
    open (FH, $filename) or die ... local $/ = "\n\n"; # read objects, not lines while (<FH>) { chomp($_); # remove \n\n at the end of the object s/\n //g; # care for continuation lines, e.g. kill them my @tmpLines = split(/\n\n/, $_); my %data = (); foreach my $line (@tmpLines) { my ($attr, $value) = split(/\s*=\s*/, $line, 2); # do something with $attr and $value, e.g. push (@{ $data{$attr} }, $value); } # foreach # e.g. extract first uid as dn my $dn = shift( @{ $data{uid} } ); print "dn: $dn\n"; foreach my $attr (keys %data) { foreach my $value (@{ $data{$key} }) { # perhaps you should care to create continuation lines # or better write the ldif with Net::LDAP::LDIF print "$attr: $value\n"; } } print "\n"; # object separator } # while close (FH);
    or the like... (not tested)

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: fixing broken ldif files (replacing = with : )
by bronto (Priest) on Dec 09, 2004 at 13:14 UTC

    Maybe you just need to add -L options to ldapsearch

    -L print responses in LDIFv1 format -LL print responses in LDIF format without comments -LLL print responses in LDIF format without comments

    Ciao!
    --bronto


    In theory, there is no difference between theory and practice. In practice, there is.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://413421]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 09:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found