2

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: scott@example3.org
zimbraLastLogonTimestamp: 20161019154312Z
zimbraPrefMailForwardingAddress: office@example3.org

# info, people, example5.org
dn: uid=info,ou=people,dc=example5,dc=org
zimbraMailDeliveryAddress: info@example5.org
zimbraCOSId: e2zzy7e74-e4bf-462d-a4b1-7d7b23c31fbe
zimbraPrefMailForwardingAddress: asmith@example5.org
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:

scott@example3.org,20161019154312Z,office@example3.org info@example5.org,20181011075800Z,asmith@example5.org

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

dj423
  • 23
  • 4
  • 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. – U880D Jul 06 '23 at 20:14
  • Added my search query. FWIW: I can get the information, just trying to parse/format the values to a delimited table. Thanks! – dj423 Jul 06 '23 at 20:26
  • @dj423 See the answe below. – Valentin Bajrami Jul 06 '23 at 20:27
  • 1
    You rock, this is fantastic, I searched for days, this is perfect for my use case! – dj423 Jul 06 '23 at 23:49

1 Answers1

2

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

Valentin Bajrami
  • 4,045
  • 1
  • 18
  • 26