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"; } }

In reply to fixing broken ldif files (replacing = with : ) by acser

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.