Search code examples
filelistparsingawkldif

Generating Ldifs with Help from AWK (can't parse input file)


Folks I need to add a serie of entries on an ldap based on a 10k records list. I have the following code:

awk -v uid=999905284 '
{
print "dn: uid="$0",ou=aaa,ou=bbb,dc=br\n
uid: "$0"\n
sn: "$0"\n
cn: "$0"\n
mail: "$0"@grupos.a.br\n
description: "$0"\n
phpgwAccountType: l\n
phpgwAccountStatus: A\n
uidNumber: "uid++"\n
gidNumber: 0\n
deliveryMode: forwardOnly\n
accountStatus: active\n
defaultMemberModeration: 1\n"
}' list

The input of list is as follows:

list-a
list-b
...
list-n

Sample expected output:

dn: uid=list-a,ou=aaa,ou=bbb,dc=br\n
uid: list-a
sn: list-a
cn: list-a
mail: list-a@grupos.a.br
description: list-a
phpgwAccountType: l
phpgwAccountStatus: A
uidNumber: 999905284
gidNumber: 0
deliveryMode: forwardOnly
accountStatus: active
defaultMemberModeration: 1


dn: uid=list-b,ou=aaa,ou=bbb,dc=br\n
uid: list-b
sn: list-b
cn: list-b
mail: list-b@grupos.a.br
description: list-b
phpgwAccountType: l
phpgwAccountStatus: A
uidNumber: 999905285
gidNumber: 0
deliveryMode: forwardOnly
accountStatus: active
defaultMemberModeration: 1

I would expect an output where the $0 would be replaced by the name of the lists thou it is not working.

Thanks in advance!


Solution

  • awk doesn't allow you to split quoted strings over two lines, unless you mark each line as a continuation by putting a \ at the end. Since you are already including \n in the output explicitly, you can just fix things up by putting the continuation markers everywhere a quoted string is split:

    awk -v uid=999905284 '
    {
    print "dn: uid="$0",ou=aaa,ou=bbb,dc=br\n\
    uid: "$0"\n\
    sn: "$0"\n\
    cn: "$0"\n\
    mail: "$0"@grupos.a.br\n\
    description: "$0"\n\
    phpgwAccountType: l\n\
    phpgwAccountStatus: A\n\
    uidNumber: "uid++"\n\
    gidNumber: 0\n\
    deliveryMode: forwardOnly\n\
    accountStatus: active\n\
    defaultMemberModeration: 1\n"
    }' list