I'm working on a bash script to process various LDAP queries into pipe delimited files. Some of the results records do not include all attributes, and the data for each record does not retun in the same attribute order for each record. I've scripted to ensure all records have the 4 necessary attributes, and am now trying use awk to reorder the fields of the output records to all match an established order. Below is a sample set of records I'm looking to process with the first record representing the desired order/column heads.
cn: User ID
displayName: Display Name
LastLoginTime: Last Login
ExpirationDate: Exp Date
cn: mf_mdsa
displayName: NONE
ExpirationDate: 00000000000000
LastLoginTime: 20201220212738
displayName: NONE
cn: mf_cs
ExpirationDate: 00000000000000
LastLoginTime: 20201220212704
displayName: NONE
ExpirationDate: 00000000000000
LastLoginTime: 20181009205555
cn: OPERATOR
ExpirationDate: 00000000000000
displayName: HENNIE VAN DEVENTER
LastLoginTime: 20181030201447
cn: hvdevent
cn: A0S
displayName: LARA EVERWINE
ExpirationDate: 20190612001951
LastLoginTime: 20190313182136
cn: SODS822
ExpirationDate: 00000000000000
displayName: JAMES SIMS
LastLoginTime: 20210104000757
I'm calling an awk script as follows and outputting to a file
BEGIN {FS=": "; OFS="|"}
$1 == "cn" {cn = $2}
$1 == "displayName" {displayName = $2}
$1 == "LastLoginTime" {LastLoginTime = $2}
$1 == "ExpirationDate" {
print cn,displayName,LastLoginTime,$2}
While the desired reordering seems to be happening, fields values from one record are bleeding over into the next. I'm supposing it has something to do with how the field label and values are being added to the variables to feed the print, but I've not been able to figure out how to process each record correctly before moving to the next
User ID|Display Name|Last Login|Exp Date
mf_mdsa|NONE|Last Login|00000000000000
mf_cs|NONE|20201220212738|00000000000000
mf_cs|NONE|20201220212704|00000000000000
OPERATOR|NONE|20181009205555|00000000000000
A0S|LARA EVERWINE|20181030201447|20190612001951
SODS822|LARA EVERWINE|20190313182136|00000000000000
SODS822|JAMES SIMS|20190313182136|00000000000000