Reputation: 11
I retrieve from LDAP all kerberos users (but only some of their variables e.g. kerberosid,location, mail, name, surname,... ) into a flat file (delimiter is pipeline).
I tried to use LDAP syntax from LDAPSEARCH into table format but I am getting mixed values I mean (I can see e.g. the same name in next line, but next line in the file starts with second LDAP record, etc.) Performance is important.
I would prefer to have even empty string in position where value is missing. I am not sure if this is not happening because in some previous records whole index (variable e.g. mail) is missing and from this position all is shifted...
Code block from the link :
$ ldapsearch -x -D "cn=something" | awk -v OFS=',' '{split($0,a,": ")} /^mail:/{mail=a[2]} /^uidNumber:/{uidNumber=a[2]} /^uid:/{uid=a[2]} /^cn/{cn=a[2]; print uid, uidNumber,cn , mail}' > ldap_dump.csv
Ldap search output:
dn: xxxxx
objectClass: top
mail: 11111
uidNumber: 222 222
uid: 333, 3333
cn: 44444
somethingnext: 5555
dn: new records
objectClass: top
mail: aaaaa
uidNumber: bbbbb
uid: cccc, cccc
cn: ddddd
somethingnext: eeeee
each line is on new line and between records is empty new line
Needed output in a file should be :
11111|222 222|333, 3333|44444
aaaaa|bbbbb|cccc, cccc|ddddd
Upvotes: 1
Views: 2553
Reputation: 26491
Use the following awk
:
awk 'BEGIN{RS=""; OFS="|"}
{ mail = cn = uidNumber = uid = ""
for(i=1;i<=NF;++i) {
if ($i ~ /^mail:/) { mail=substr($i,7) }
else if ($i ~ /^uidNumber:/) { uidNumber=substr($i,12) }
else if ($i ~ /^uid:/ ) { uid=substr($i,6) }
else if ($i ~ /^cn:/ ) { cn=substr($i,5) }
}
print mail,uidNumber,uid,cn
}'
A corrected version of your original version is:
awk 'BEGIN{FS=": *"; OFS="|"}
(NF==0) { print uid,uidNumber,cn,mail }
(NF==0) { mail = uidNumber = uid = cn = ""; next }
{split($0,a,": ")}
/^mail:/{mail=a[2]}
/^uidNumber:/{uidNumber=a[2]}
/^uid:/{uid=a[2]}
/^cn/{cn=a[2]}
END{ print uid,uidNumber,cn,mail }'
Upvotes: 1