I'm trying to manipulate an ldif file with multiple entries. My purpose is to parse this existing ldif file, extracting the "givenName" and "sn" attributes, so to generate a "mail" attribute. I was thinking about AWK or Sed, but unfortunately I'm not an expert on the two nice tools. An example:
original file
dn: cn=fremer, ou=people, dn=domain, dn=com
cn: fremer
givenName: Freddy
sn: Mercury
dn: cn=markno, ou=people, dn=domain, dn=com
cn: markno
givenName: Mark
sn: Knopfler
Output:
dn: cn=fremer, ou=people, dn=domain, dn=com
mail: [email protected]
dn: cn=markno, ou=people, dn=domain, dn=com
mail: [email protected]
The dn is needed since I will take the resulting ldif and pass it to "ldapadd" for LDAP update. Any suggestion or hint on where should I look at? Thank you!
You can do this with an awk script
to use, save the above in a file e.g. awkscript and make it executable then
Given your input this script outputs
For multi-line stuff, I always fall back to Perl or something else that allows me to write actual data structures in a semi-readable fashion. You can actually write readable Perl code; I've never been able to read an awk command once it gets past a couple-dozen characters. Not at all saying it can't be done; I just don't know how to do it.
The additional benefit of Perl is that you can find (or already have) an LDAP/LDIF module so you don't have to parse this yourself. The potential downside to Perl is having to pick one of those modules. Generically, anything with "Simple" in the name will probably be your best starting place.