Score:2

Possible to parse ldapsearch output?

tm flag

I have an ldapsearch query to pull some data of an email system, I am trying to parse the data into something I can analyze in a table/flat file, and generate reports on the accounts.

Sample of the ldapsearch output:

# scott, people, example3.org
dn: uid=scott,ou=people,dc=example3,dc=org
zimbraCOSId: 3f5exxxf-08eb-439a-8d93-cef26b03b722
zimbraMailDeliveryAddress: [email protected]
zimbraLastLogonTimestamp: 20161019154312Z
zimbraPrefMailForwardingAddress: [email protected]

# info, people, example5.org
dn: uid=info,ou=people,dc=example5,dc=org
zimbraMailDeliveryAddress: [email protected]
zimbraCOSId: e2zzy7e74-e4bf-462d-a4b1-7d7b23c31fbe
zimbraPrefMailForwardingAddress: [email protected]
zimbraLastLogonTimestamp: 20181011075800Z

The search command used to pull the test data:

ldapsearch -H $ldap_master_url -w $zimbra_ldap_password -D $zimbra_ldap_userdn -S -LLL -x "(&(objectClass=zimbraAccount)(!(objectclass=zimbraCalendarResource)))" zimbraMailDeliveryAddress zimbraLastLogonTimestamp zimbraPrefMailForwardingAddress | awk '$1=$1' RS= ORS='\n' OFS=';'

Here is what I tried:

| awk '$1=$1' RS= ORS='\n' OFS=';'

Then I also used grep to prune it down further, but not all records have the same attributes, or the same order so the output is a mess.

Output I am shooting for:

The values of the above attributes in a by row delimited style.

ie:

[email protected],20161019154312Z,[email protected] [email protected],20181011075800Z,[email protected]

Will research if sed can gather the info, since the values are in different order from record to record which seems odd. I have more data fields to add, but very difficult to parse the output.

Thank you

U880D avatar
ca flag
Does [How to create a list of email addresses from `ldapsearch` result for further processing?](https://unix.stackexchange.com/a/455074/266568) answers your question? And could you provide your search command, as well what kind of information you are looking for? It might be that other solutions could come in place.
dj423 avatar
tm flag
Added my search query. FWIW: I can get the information, just trying to parse/format the values to a delimited table. Thanks!
Valentin Bajrami avatar
br flag
@dj423 See the answe below.
dj423 avatar
tm flag
You rock, this is fantastic, I searched for days, this is perfect for my use case!
Score:2
br flag

This should be fairly straight forward using awk. Here is a solution

awk '/^zimbraMail/ {addr = $2} /^zimbraLastL/ {timestamp = $2} /^zimbraPrefMailF/ {forward = $2; print addr "," timestamp "," forward}'

What you do is use a pattern match /.../ and store the desired field in a variable and finally print it out

dj423 avatar
tm flag
Thank you! This is perfect.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.