ldif-to-csv.sh
· 1.3 KiB · Bash
Bruto
#!/bin/bash
#
# Converts LDIF data to CSV.
# Doesn't handle comments very well. Use -LLL with ldapsearch to remove them.
#
# 2010-03-07
# dsimmons@squiz.co.uk
#
# 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 CSV. LDIF data is read from stdin."
echo ""
exit 99
fi
ATTRS="$*"
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)
attr=${line%%:*}
value=${line#*: }
# 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
| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Converts LDIF data to CSV. |
| 4 | # Doesn't handle comments very well. Use -LLL with ldapsearch to remove them. |
| 5 | # |
| 6 | # 2010-03-07 |
| 7 | # dsimmons@squiz.co.uk |
| 8 | # |
| 9 | |
| 10 | # Show usage if we don't have the right params |
| 11 | if [ "$1" == "" ]; then |
| 12 | echo "" |
| 13 | echo "Usage: cat ldif.txt | $0 <attributes> [...]" |
| 14 | echo "Where <attributes> contains a list of space-separated attributes to include in the CSV. LDIF data is read from stdin." |
| 15 | echo "" |
| 16 | exit 99 |
| 17 | fi |
| 18 | |
| 19 | ATTRS="$*" |
| 20 | |
| 21 | c=0 |
| 22 | while read line; do |
| 23 | |
| 24 | # Skip LDIF comments |
| 25 | [ "${line:0:1}" == "#" ] && continue; |
| 26 | |
| 27 | # If this line is blank then it's the end of this record, and the beginning |
| 28 | # of a new one. |
| 29 | # |
| 30 | if [ "$line" == "" ]; then |
| 31 | |
| 32 | output="" |
| 33 | |
| 34 | # Output the CSV record |
| 35 | for i in $ATTRS; do |
| 36 | |
| 37 | eval data=\$RECORD_${c}_${i} |
| 38 | output=${output}\"${data}\", |
| 39 | |
| 40 | unset RECORD_${c}_${i} |
| 41 | |
| 42 | done |
| 43 | |
| 44 | # Remove trailing ',' and echo the output |
| 45 | output=${output%,} |
| 46 | echo $output |
| 47 | |
| 48 | # Increase the counter |
| 49 | c=$(($c+1)) |
| 50 | fi |
| 51 | |
| 52 | # Separate attribute name/value at the semicolon (LDIF format) |
| 53 | attr=${line%%:*} |
| 54 | value=${line#*: } |
| 55 | |
| 56 | # Save all the attributes in variables for now (ie. buffer), because the data |
| 57 | # isn't necessarily in a set order. |
| 58 | # |
| 59 | for i in $ATTRS; do |
| 60 | if [ "$attr" == "$i" ]; then |
| 61 | eval RECORD_${c}_${attr}=\"$value\" |
| 62 | fi |
| 63 | done |
| 64 | |
| 65 | done |
| 66 |