LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Enterprise (https://www.linuxquestions.org/questions/linux-enterprise-47/)
-   -   Sort attributes by name for each entry in ldapsearch output (ldap database export) (https://www.linuxquestions.org/questions/linux-enterprise-47/sort-attributes-by-name-for-each-entry-in-ldapsearch-output-ldap-database-export-716914/)

llattan 04-04-2009 08:50 PM

Sort attributes by name for each entry in ldapsearch output (ldap database export)
 
I need to sort attributes by name for each entry in ldapsearch output
(-S "" sort entries by name, but not their attributes)

I need this to compare consistency of 2 ldap databases after the wan connection link up (in multimaster configuration). Because the time of writing for each attribute could differ, the order could not be the same.


My "export" script

HOSTNAME=localhost
BINDDN="cn=manager,dc=example,dc=com"
BASE="dc=example,dc=com"
PASSWORD=`cat /etc/ldap.secret`

/usr/bin/ldapsearch -LLL -x -h $HOSTNAME -D "$BINDDN" -w "$PASSWORD" -b "$BASE" -S ""


Thanks in advance.
Leandro.

remigiusz 08-03-2023 08:28 AM

Could not find it either. Ended up with brute force solution with sorting within sections of ldapsearch output like:

/usr/bin/ldapsearch -LLL (...) | sort_within_sections.sh

where sort_within_sections.sh looks like:
Quote:

$ cat sort_within_sections.sh
#!/bin/bash

# Author: Remigiusz Boguszewicz
# Purpose: Sort with sections, seemed like a job for oneliner, but I could not
# find an elegant solution, so this script had to be born
# Input: standard input or file provided as parameter
# Output: standard output

V_TMP_FILE="/tmp/sort_within_sections.tmp"
rm -f $V_TMP_FILE

while read line
do
if [ -z "$line" ]; then
#echo "[debug] we have separator, which is empty line"
#echo "[debug] print everything from section now, then print separator"
if [ -f $V_TMP_FILE ]; then
#echo "[debug] File $V_TMP_FILE exists. Printing its contents sorted."
cat $V_TMP_FILE | sort
rm -f $V_TMP_FILE
fi
echo "$line"
else
#echo "[debug] append line to temp file"
echo "$line" >> $V_TMP_FILE
fi
done < "${1:-/dev/stdin}"
---
Regards
Remigiusz Boguszewicz


All times are GMT -5. The time now is 12:56 AM.