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: [email protected], ou=persons,ou=pp,dc=abc,dc=com
dn: [email protected], ou=persons,ou=pp,dc=abc,dc=com
dn: [email protected], ou=persons,ou=pp,dc=abc,dc=com
dn: [email protected], ou=persons,ou=pp,dc=abc,dc=com

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

dn: [email protected], ou=persons,ou=pp,dc=abc,dc=com
changetype: modify
add: mail
mail: [email protected]

dn: [email protected], ou=persons,ou=pp,dc=abc,dc=com
changetype: modify
add: mail
mail: [email protected]

dn: [email protected], ou=persons,ou=pp,dc=abc,dc=com
changetype: modify
add: mail
mail: [email protected]

dn: [email protected], ou=persons,ou=pp,dc=abc,dc=com
changetype: modify
add: mail
mail: [email protected]

For each dn entry in input file, I want to extract cn part ([email protected]) 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 ([email protected]) 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: [email protected], 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.

2 Answers 2

1

This might work for you:

sed -e 's/.*cn=\([^,]*\).*/&\nchangetype: modify \nadd: mail \nmail: \1/'
1
  • Is enough s/cn=\([^,]*\).*/
    – Costas
    Jul 24, 2015 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .