Suppressing empty output lines when using ldapsearch

Problem

You want to use ldapsearch to search from a file (file contains domains and ldap contains emails), but you want to suppress empty output lines (option -LLL does not seem to suppress them).

Solution

Use grep -v ‘^$’ like

ldapsearch -LLL -H "ldaps://ldap_url:nnnn" -D "cn=Directory Manager" -w $LDP -b "ou=ou_name,dc=domain,dc=com" -s sub -f denyDomains.txt  "(mail=*@%s)" uid mail status | grep -v '^$' > denyDomainsResults.txt

LDAP search using a file as input

Problem

You would like to do a search for a specific LDAP attribute (ie email) using a file (emails.txt) with the list of emails you want to search for.

Solution

After exporting the password (LDP) you can use the following to do the search and output the results into a file (search_results.txt)

ldapsearch -LLL _H "ldaps://ldap_url:port" -D "cn=User_name" -w $LDP -b "ou=ou_name,dc=dc_name,dc=domain_name" -s sub -f emails.txt "(mail_attribute=%s)" uid mail_attribute other_attribute > search_results.txt