Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


dn: cn=fremer, ou=people, dn=domain, dn=com

dn: cn=markno, ou=people, dn=domain, dn=com

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!

share|improve this question
up vote 3 down vote accepted

You can do this with an awk script

#!/bin/awk -f
/^dn:/ {split($0,dname,", dn=");print $0 }
/^cn:/ { cn = $2 }
/^givenName:/ { gn = tolower($2) }
/sn:/ { sn = tolower($2) ; printf("mail: %s.%s@%s.%s\n\n",gn,sn,dname[2],dname[3] ) }

to use, save the above in a file e.g. awkscript and make it executable then

./awkscript datafile

Given your input this script outputs

dn: cn=fremer, ou=people, dn=domain, dn=com

dn: cn=markno, ou=people, dn=domain, dn=com
share|improve this answer
Now see, that's damn near comprehensible. Bravo, sir. – mfinni Aug 22 '11 at 20:52
@minni: It's the only way I can do this sort of thing, if I try to make it 'compact' my head explodes. – Iain Aug 22 '11 at 20:58
Thank you lain, the script is simply perfect. Now I'm studying it, awk is so powerful. – Rilik84 Aug 23 '11 at 7:57

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.

share|improve this answer
Thanks for your suggestion as well! Unfortunately I'm not a programmer, but as far as I could see, you're definitely right in regard to the readability. Awk syntax is quite cryptical. – Rilik84 Aug 23 '11 at 9:18
Perl is a scripting language. To start, you can treat it like a more powerful 'sh' or 'bash'. No compiling, so it's much less "programmer-y" and it is definitely suited to sysadmin tasks. – mfinni Aug 23 '11 at 20:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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