Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Transform LDIF file with awk 1

Status
Not open for further replies.

maleks

Technical User
Aug 13, 2009
2
SE
Hi
I'm trying to transform one LDIF file into another format but I have trouble understanding awk even after reading several tutorials. Any ideas on how to do it? Thanks.

I have one file that basically looks like this:

# Add new values: addressBookContainer
dn: cn=Address-Book-Container,cn=Schema,cn=Configuration,dc=X
changetype: ntdsschemamodify
add: mayContain
mayContain: msExchAddressListOU
mayContain: msExchEnableInternalEvaluator
mayContain: msExchMinAdminVersion
-

# Add new values: computer
dn: cn=Computer,cn=Schema,cn=Configuration,dc=X
changetype: ntdsschemamodify
add: mayContain
mayContain: logRolloverInterval
mayContain: monitoredConfigurations
mayContain: monitoredServices
-

and I need it to look like this, every mayContain must have the lines:
"changetype: ntdsschemamodify
add: mayContain:"
before it and on the line after it it must have:
"-"

# Add new values: addressBookContainer
dn: cn=Address-Book-Container,cn=Schema,cn=Configuration,dc=X
changetype: ntdsschemamodify
add: mayContain
mayContain: msExchAddressListOU
-
changetype: ntdsschemamodify
add: mayContain
mayContain: msExchEnableInternalEvaluator
-
changetype: ntdsschemamodify
add: mayContain
mayContain: msExchMinAdminVersion
-

# Add new values: computer
dn: cn=Computer,cn=Schema,cn=Configuration,dc=X
changetype: ntdsschemamodify
add: mayContain
mayContain: logRolloverInterval
-
changetype: ntdsschemamodify
add: mayContain
mayContain: monitoredConfigurations
-
changetype: ntdsschemamodify
add: mayContain
mayContain: monitoredServices
-

If that is not possible then each mayContain can look like this:

dn: cn=DN_FROM_THE_RECORDS_FIRST_LINE,cn=Schema,cn=Configuration,dc=X
changetype: ntdsschemamodify
add: mayContain
mayContain: logRolloverInterval
-
 
Try this:

Code:
awk '
        [green]/^changetype:/[/green] { ct=[blue]$0[/blue]; [b]next[/b] }
        [green]/^add:/[/green] { add=[blue]$0[/blue]; [b]next[/b] }
        [green]/^mayContain:/[/green] { [b]print[/b] ct [blue]ORS[/blue] add [blue]ORS[/blue] [blue]$0[/blue] [blue]ORS[/blue] [red]"[/red][purple]-[/purple][red]"[/red]; [b]next[/b] }
        [green]/^-/[/green] { [b]next[/b] }   [gray]# do not print extra dashes[/gray]
        1               [gray]# print any other input lines[/gray]
' inputfile > outputfile

Annihilannic.
 
Thanks alot, your awk program is so far from what I have tried to produce, just amazing!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top