#!/bin/sh
{ tee /dev/fd/3 <&4&:|
grep -q '[^[:space:]]' &&
cat - /dev/fd/4 <&3
} 3<<"" 4<&0 | another_few_commands
# cause i dont know how else to end this with a blank line
Это скрипт, который считывает LDIF из STDIN и выводит в формате CSV
#!/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
Щелкните здесь , чтобы узнать больше
Я вижу некоторые серьезные недостатки в таких простых скриптах, как этот:
Если вы не хотите исправлять это самостоятельно после прочтения RFC 2849 , я рекомендую реализовать короткий скрипт Python с использованием модуля python -ldap sub -ldif и встроенный -в модуль csv .
с Миллером(http://johnkerl.org/miller/doc)и sed очень короткий и простой
sed 's/://g' input.txt | mlr --x2c cut -x -f CC
дает вам
AA,BB,DD
User11_Value1,User11_Value2,User11 Space Value4
User22_Value1,User22_Value2,User22 Space Value4
Затем я удаляю :
, чтобы получить один из родных форматов ввода Миллера (XTAB ), затем конвертирую XTAB в CSV с помощью --x2c
и в конце удаляю CC
поле с вырезом.