Zuletzt aktiv 1 week ago

bash script to convert LDIF data to CSV.

scusi hat die Gist bearbeitet 10 months ago. Zu Änderung gehen

Keine Änderungen

Daniel Simmons hat die Gist bearbeitet 15 years ago. Zu Änderung gehen

1 file changed, 65 insertions

ldif-to-csv.sh(Datei erstellt)

@@ -0,0 +1,65 @@
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
Neuer Älter