2

I want to create a ldif modify file to update the users on LDAP.

I have an input file(alluserdns.ldif) with list of all Dn entries in ldap:

dn: cn=testuser2@abc.com, ou=persons,ou=pp,dc=abc,dc=com
dn: cn=testuser3@nabc.com, ou=persons,ou=pp,dc=abc,dc=com
dn: cn=testuser4@abc.com, ou=persons,ou=pp,dc=abc,dc=com
dn: cn=testuser5@abc.com, ou=persons,ou=pp,dc=abc,dc=com

I want to create a file that will have following format:

dn: cn=testuser2@abc.com, ou=persons,ou=pp,dc=abc,dc=com
changetype: modify
add: mail
mail: testuser2@abc.com

dn: cn=testuser3@nabc.com, ou=persons,ou=pp,dc=abc,dc=com
changetype: modify
add: mail
mail: testuser3@nabc.com

dn: cn=testuser4@abc.com, ou=persons,ou=pp,dc=abc,dc=com
changetype: modify
add: mail
mail: testuser4@abc.com

dn: cn=testuser5@abc.com, ou=persons,ou=pp,dc=abc,dc=com
changetype: modify
add: mail
mail: testuser5@abc.com

For each dn entry in input file, I want to extract cn part (testuser2@abc.com) and use that as a mail attribute

changetype: modify
add: mail
mail:

the above 3 lines to be added after each line in input file and use extracted cn value (testuser2@abc.com) to form last line.

So far I have tried following to add above 3 constant lines after each dn entry in input file

sed -e 's/$/\n changetype: modify \n   add: mail \n    mail:/' -i alluserdns.ldif

Output I get for each dn is:

     dn: cn=testuser2@abc.com, ou=persons,ou=pp,dc=abc,dc=com
     changetype: modify
     add: mail
     mail:

Now i need to extract the cn part and use it in 3rd line after mail : attribute.

1

This might work for you:

sed -e 's/.*cn=\([^,]*\).*/&\nchangetype: modify \nadd: mail \nmail: \1/'
  • Is enough s/cn=\([^,]*\).*/ – Costas Jul 24 '15 at 15:30
0

You can do it with awk (only tested with GNU awk) like

awk  '{print $0;match($2, /=(.*),/, arr); if(arr[1]!="") {print "changetype: modify\nadd:mail\nmail: " arr[1] }}' <input file>

This will print each line print $0, then use match to pull out the email address. Then it will print the stanza you want, assuming it found an email address to print. If it did not find the address it will not print that stanza.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.