3

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

4
  • Does How to create a list of email addresses from ldapsearch result for further processing? 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. Commented Jul 6, 2023 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! Commented Jul 6, 2023 at 20:26
  • @dj423 See the answe below. Commented Jul 6, 2023 at 20:27
  • 1
    You rock, this is fantastic, I searched for days, this is perfect for my use case! Commented Jul 6, 2023 at 23:49

1 Answer 1

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

1
  • 2
    Thank you! This is perfect. Commented Jul 6, 2023 at 23:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.