I recently wrote a bash script that had to parse the output of ldapsearch results. The script works, but I imagine there is a more efficient way to accomplish this.
The script executes an ldapsearch command, which outputs multiple records that are in a multiline format. Each record is separated by a blank line. What I ended up doing was the following:
- add a delimitating character to the end of each line
- Add the string 'DELIM' to blank lines
- trimmed all new lines
- Replaced 'DELIM' with a new line
What this effectively did was turn the multiline output of ldapsearch to multiple lines of delimited separated values. I then use cut twice to parse the lines (once to split the delimiter, and then again to spit the output of the ldap result)
Here is the code:
while IFS= read -r line ; do
dn=$(echo "$line" | cut -d '#' -f 1 | cut -d " " -f 2)
uid=$(echo "$line" | cut -d '#' -f 2 | cut -d " " -f 2)
uidNumber=$(echo "$line" | cut -d '#' -f 3 | cut -d " " -f 2)
gidNumber=$(echo "$line" | cut -d '#' -f 4 | cut -d " " -f 2)
# Code emitted since it's not relevant
done < <(ldapsearch -x -H "$ldap_server" -D 'cn=Directory Manager' -w $ds_password -b "$searchbase" -LLL uid uidNumber gidNumber | sed 's/$/#/g' | sed 's/^#$/DELIM/g' | tr -d '\n' | sed 's/DELIM/\n/g')
The output of the ldapsearch command is the following
dn: uid=userone,ou=People,dc=team,dc=company,dc=local
uid: userone
uidNumber: 5000
gidNumber: 5000
dn: uid=usertwo,ou=People,dc=team,dc=company,dc=local
uid: usertwo
uidNumber: 5001
gidNumber: 5001
Is there a more efficient way to accomplish this? Specifically one that doesn't use piping so extensively?