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:
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?
You can play around with awk
ldap search | awk '$1=$1' RS= ORS='\n' OFS='#'
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
RS=
Set record selector to nothing (will set awk to block mode) (normally every line)
ORS='\n'
Set output record selector to a new line
OFS='#'
set output field separator to #
$1=$1
recreate the line and print it.
This may be more secure to use:
awk '{$1=$1}1' RS= ORS='\n' OFS='#'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With