任务:LDIF到CSV格式转换

试撰写一脚本或应用程序,输入为一个LDIF文件,输出为CSV文件。输入LDIF文件中contactPerson不会有多于6个值。

要求:
提供的LDIF文件都符合RFC2849定义,要求生成的CSV文件符合RFC4180定义。
可以使用任何编程语言,可以不读LDIF文件而直接从LDAP数据库中查询。
转换程序必须工作可靠,我们将用4000条数据测试转换程序。
转换程序必须节约内存,处理3条数据和处理4000条数据使用大致同样的内存。
要求必须正确处理值中带有引号、冒号、等号、&符号、反斜线、竖线、逗号和换行的数据;
转换程序的工作时间不做要求。如果处理4000条数据需用很长时间,需在stderr上给出进度信息。
鼓励使用别人的代码解决问题。以上需求都满足的情况下,应正确合理使用因特网上找到的代码和工具。自己写的代码越少越好。

提示:
如果需从头学习一种语言完成此任务,能完成此任务的最简单的计算机语言可能是awk。
使用脚本类语言可能会轻松些。
按RFC相关规定,汉字会以UTF-8字符集用Base64编码,小心汉字的处理是否正确。
小心特殊符号。使用OpenOffice打开CSV文件以检查是否正确。错误的CSV文件常常引起记录总数不对。
LDIF文件的格式是在RFC2849文件中定义的:   http://www.ietf.org/rfc/rfc2849.txt
CSV文件的格式是在RFC4180文件中定义的:    http://www.ietf.org/rfc/rfc4180.txt

请看下面两个列子,找出从输入按参数转换为输出的规律,并撰写转换程序。


例一:输入文件为:

dn: o=Shenyang University GBS Academy Automotive Industry (SGA),c=CN
o: Shenyang University GBS Academy Automotive Industry (SGA)
c: CN
st: Liaoning
l: Shenyang
street: Wanghua Nanjie 21, Dadong District Shenyang
businessCategory: Vocational Education
description: Manufacturer of washing machine and components

参数为:attributes=o,businessCategory,c,st,l,street contactPerson=cn,sn,givenName,mail

输出文件为:

o,businessCategory,c,st,l,street
Shenyang University GBS Academy Automotive Industry (SGA),Vocational Education,DE,Liaoning,Shenyang,Wanghua Nanjie 21, Dadong District Shenyang

例二:输入文件为:

dn: o=KAEFER Insulation (Shanghai) Co.,Ltd.,c=DE
o: KAEFER Insulation (Shanghai) Co.,Ltd.
c: DE
contactPerson: cn=Horst Koerner,o=KAEFER Insulation (Shanghai) Co.,Ltd.,c=DE
contactPerson: cn=Pamela Tsao,o=KAEFER Insulation (Shanghai) Co.,Ltd.,c=DE
businessCategory: Iron/Steel/Metal Processing, Products

dn: cn=Pamela Tsao,o=KAEFER Insulation (Shanghai) Co.,Ltd.,c=DE
cn: Pamela Tsao
givenName: Tsao
sn: Pamela
mail: [email protected]
telephoneNumber: +86-21-6712 0218(106)

dn: cn=Horst Koerner,o=KAEFER Insulation (Shanghai) Co.,Ltd.,c=DE
cn: Horst Koerner
givenName: Horst
sn: Koerner
mail: [email protected]

参数为:attributes=o,businessCategory,c,st,l,street contactPerson=cn,sn,givenName,mail
输出文件为:

o,businessCategory,c,st,l,street,1.cn,1.sn,1.givenName,1.mail,2.cn,2.sn,2.givenName,2.mail
KAEFER Insulation (Shanghai) Co.,Ltd.,Iron/Steel/Metal Processing, Products,DE,,,,Horst Koerner,Koerner,Horst,[email protected],Pamela Tsao,Tsao,Pamela,[email protected]