Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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: freddy.mercury@domain.com

dn: cn=markno, ou=people, dn=domain, dn=com
mail: mark.knopfler@domain.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

2 Answers 2

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
mail: freddy.mercury@domain.com

dn: cn=markno, ou=people, dn=domain, dn=com
mail: mark.knopfler@domain.com
share|improve this answer
    
Now see, that's damn near comprehensible. Bravo, sir. –  mfinni Aug 22 '11 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. –  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

 
discard

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.