4

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!

2 Answers 2

3

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
mail: [email protected]

dn: cn=markno, ou=people, dn=domain, dn=com
mail: [email protected]
3
  • Now see, that's damn near comprehensible. Bravo, sir.
    – mfinni
    Aug 22, 2011 at 20:52
  • 1
    @minni: It's the only way I can do this sort of thing, if I try to make it 'compact' my head explodes.
    – user9517
    Aug 22, 2011 at 20:58
  • Thank you lain, the script is simply perfect. Now I'm studying it, awk is so powerful. Aug 23, 2011 at 7:57
2

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.

2
  • 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. Aug 23, 2011 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, 2011 at 20:51

You must log in to answer this question.

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