Hey guys.. I am not sure if this is the right place to post this - but here goes. I need to manipulate an openldap export to match a different schema so that I can import into that system. Basically - its just text manipulation. I have gotten alot of it done just by using simple sed, but I am sorta stuck on the following. here is an example export:
dn: mailRoutingAddress=joe.smith@domain.com,ou=People, dc=domain,dc=com
changetype: add
objectClass: user
sn: Smith
givenName: Joe
cn: Joe Smith
telephoneNumber: (304)555-5555
facsimileTelephoneNumber: (304)555-5555
title: LCM 2
uid: joe.smith
dn: mailRoutingAddress=jjones@domain.com,ou=People,dc= domain,dc=com
changetype: add
objectClass: user
sn: Jones
givenName: Jenny
cn: Jenny Jones
uid: jjones
telephoneNumber: (406)555-5555
facsimileTelephoneNumber: (406)555-5555
title: LCM 2
--snip--
There are 4000 of these entries, each with line breaks between them. What I want to do is automate changing of the dn line to get rid of the 'mailRoutingAddress bit and change it to the cn. so once the entries have been 'fixed' they will read like this:
dn: cn=Joe Smith,ou=People,dc=domain,dc=com
changetype: add
objectClass: user
sn: Smith
givenName: Joe
cn: Joe Smith
telephoneNumber: (304)555-5555
facsimileTelephoneNumber: (304)555-5555
title: LCM 2
uid: joe.smith
dn: cn=Jenny Jones,ou=People,dc=domain,dc=com
changetype: add
objectClass: user
sn: Jones
givenName: Jenny
cn: Jenny Jones
uid: jjones
telephoneNumber: (406)555-5555
facsimileTelephoneNumber: (406)555-5555
title: LCM 2
Im a little stumped at how I could do this.. I suppose I could write a for loop to go through and change the dn - but i really havent the slightest idea where to start! Any help would be appreciated.
I managed to figure out how to edit one stanza at a time (with one 'user' entry in a file called file.ldif).. like this:
#!/bin/bash
cn=`grep cn file.ldif | awk -F: {'print $2'}|cut -c2-`
sed -e "1i dn: cn=\\$cn,ou=People,dc=domain,dc=com" file.ldif
the file.dif looks like:
changetype: add
objectClass: user
sn: Jones
givenName: Jenny
cn: Jenny Jones
uid: jjones
telephoneNumber: (406)555-5555
facsimileTelephoneNumber: (406)555-5555
title: LCM 2
beforehand.. running the script outputs outputs an ldif like this:
dn: cn=Jenny Jones,ou=People,dc=domain,dc=com
changetype: add
objectClass: user
sn: Jones
givenName: Jenny
cn: Jenny Jones
uid: jjones
telephoneNumber: (406)555-5555
facsimileTelephoneNumber: (406)555-5555
title: LCM 2
How do I get it to roll through each stanza and have it modify, or create dn like this, and output it to a new (or the same) file with all the edits)?
-
---------- Post updated at 12:56 PM ---------- Previous update was at 11:26 AM ----------
So I almost have it..
Ive made a quick user list - only 2 users and tried this:
contents of users file:
dawn.powers@domain.com
pat.brown@domain.com
script:
#!/bin/bash
for i in `cat users`
do
ldapsearch -x -h ldapmaster -b "ou=people,dc=domain,dc=com" mailroutingaddress=$i cn mail | grep -Ev '#|search|result|dn' > tem
pfile
cn=`grep cn tempfile| awk -F: {'print $2'}|cut -c2-`
sed -e "1i dn: cn=\\$cn,ou=People,dc=domain,dc=local" tempfile
done
The results of running the script are:
dn: cn=Dawn Powers,ou=domain,dc=rotech,dc=local
mail:
dawn.powers@domain.com
cn: Dawn Powers
dn: cn=Patricia Brown,ou=People,dc=domain,dc=local
mail:
pat.brown@domain.com
cn: Patricia Brown
Notice all of the extra carriage returns? Not sure how to get rid of those..