Skip to content

Instantly share code, notes, and snippets.

@Finkregh
Forked from dansimau/ldif-to-csv.sh
Last active September 6, 2024 11:53
Show Gist options
  • Save Finkregh/51feb486cfc257558532 to your computer and use it in GitHub Desktop.
Save Finkregh/51feb486cfc257558532 to your computer and use it in GitHub Desktop.
Shell script that reads LDIF data from STDIN and outputs as DokuWiki table.
#!/bin/bash
#
# Converts LDIF data to DW-table.
# Doesn't handle comments very well. Use -LLL with ldapsearch to remove them.
#
# 2010-03-07, [email protected]
# 2016-01-21, [email protected]
# rewrite to output dokuwiki-data
#
# Show usage if we don't have the right params
if [ "$1" == "" ]; then
echo ""
echo "Usage: cat ldif.txt | $0 <attributes> [...]"
echo "Where <attributes> contains a list of space-separated attributes to include in the DW-table. LDIF data is read from stdin."
echo ""
exit 99
fi
ATTRS="$*"
head_output="^"
for i in $ATTRS; do
eval data=\${i}
head_output=${head_output}\ ${data}\ \^
unset data
done
echo $head_output
c=0
while read line; do
# Skip LDIF comments
[ "${line:0:1}" == "#" ] && continue;
# If this line is blank then it's the end of this record, and the beginning
# of a new one.
#
if [ "$line" == "" ]; then
output="|"
# Output the CSV record
for i in $ATTRS; do
eval data=\$RECORD_${c}_${i}
output=${output}\ ${data}\ \|
unset RECORD_${c}_${i}
done
## Remove trailing ' ' and echo the output
#output=${output%,}
echo $output
# Increase the counter
c=$(($c+1))
fi
# Separate attribute name/value at the semicolon (LDIF format)
# Check for double colon and decode the base 64 results
if [[ $line == *::* ]]
then
attr=${line%%:*}
value=$(echo ${line#*: } | base64 --decode | tr -d '\0' || echo [$line] >&2)
else
attr=${line%%:*}
value=${line#*: }
fi
# Save all the attributes in variables for now (ie. buffer), because the data
# isn't necessarily in a set order.
#
for i in $ATTRS; do
if [ "$attr" == "$i" ]; then
eval RECORD_${c}_${attr}=\"$value\"
fi
done
done
@petertuharsky
Copy link

In order to get rid of the "command substitution ignored null byte in input" warnings, You should modify the line

value=echo ${line#*: } | base64 --decode

into

value=echo ${line#*: } | base64 --decode | tr -d '\0'

@Finkregh
Copy link
Author

Finkregh commented Sep 6, 2024

Thanks, updated

@petertuharsky
Copy link

Your fork seems the most capable out of all. Thank You for good work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment