Convert data from LDIF file to CSV
Need to convert selected Attributes from the block of text between blank lines in the LDIF(text) file and convert it to CSV file with comma separated delimiter, similar to below example:
Example:
LDIF file (as input):
<Blank Line>
AA: User11_Value1
BB: User11_Value2
CC: User11_Value3
DD: User11 Space Value4
<Blank Line>
AA: User22_Value1
BB: User22_Value2
CC: User22_Value3
DD: User22 Space Value4
<Blank Line>
Convert it to CSV format (as output):
AA,BB,DD
User11_Value1,User11_Value2,User11 Space Value4
User22_Value1,User22_Value2,User22 Space Value4
awk csv ldap
add a comment |
Need to convert selected Attributes from the block of text between blank lines in the LDIF(text) file and convert it to CSV file with comma separated delimiter, similar to below example:
Example:
LDIF file (as input):
<Blank Line>
AA: User11_Value1
BB: User11_Value2
CC: User11_Value3
DD: User11 Space Value4
<Blank Line>
AA: User22_Value1
BB: User22_Value2
CC: User22_Value3
DD: User22 Space Value4
<Blank Line>
Convert it to CSV format (as output):
AA,BB,DD
User11_Value1,User11_Value2,User11 Space Value4
User22_Value1,User22_Value2,User22 Space Value4
awk csv ldap
add a comment |
Need to convert selected Attributes from the block of text between blank lines in the LDIF(text) file and convert it to CSV file with comma separated delimiter, similar to below example:
Example:
LDIF file (as input):
<Blank Line>
AA: User11_Value1
BB: User11_Value2
CC: User11_Value3
DD: User11 Space Value4
<Blank Line>
AA: User22_Value1
BB: User22_Value2
CC: User22_Value3
DD: User22 Space Value4
<Blank Line>
Convert it to CSV format (as output):
AA,BB,DD
User11_Value1,User11_Value2,User11 Space Value4
User22_Value1,User22_Value2,User22 Space Value4
awk csv ldap
Need to convert selected Attributes from the block of text between blank lines in the LDIF(text) file and convert it to CSV file with comma separated delimiter, similar to below example:
Example:
LDIF file (as input):
<Blank Line>
AA: User11_Value1
BB: User11_Value2
CC: User11_Value3
DD: User11 Space Value4
<Blank Line>
AA: User22_Value1
BB: User22_Value2
CC: User22_Value3
DD: User22 Space Value4
<Blank Line>
Convert it to CSV format (as output):
AA,BB,DD
User11_Value1,User11_Value2,User11 Space Value4
User22_Value1,User22_Value2,User22 Space Value4
awk csv ldap
awk csv ldap
edited Nov 16 '18 at 0:17
Rui F Ribeiro
39.3k1479131
39.3k1479131
asked Jul 18 '18 at 21:23
RizRiz
1
1
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
This is the script which reads LDIF from STDIN and output as 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
Click here for more
Thanks Bunch... Excellent script... it worked for me. I may have to add couple of more conditions for validation, but it worked like a champ. Regards Riz
– Riz
Jul 19 '18 at 18:29
add a comment |
I see some serious deficiencies in simple scripts like this:
- no proper handling of base64-encoded data which is used for non-ASCII chars or octet strings
- no proper handling of line-wrapping
- LDAP data model has multi-valued attributes
If you don't want to fix this yourself after reading through RFC 2849 I'd recommend to implement a short Python script using the python-ldap sub-module ldif and built-in csv module.
add a comment |
with Miller (http://johnkerl.org/miller/doc) and sed is very short and easy
sed 's/://g' input.txt | mlr --x2c cut -x -f CC
gives you
AA,BB,DD
User11_Value1,User11_Value2,User11 Space Value4
User22_Value1,User22_Value2,User22 Space Value4
Whit sed I remove the :
to obtain one of the native Miller input format (XTAB), than I convert XTAB to CSV with --x2c
and at the end I remove CC
field with cut.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f457091%2fconvert-data-from-ldif-file-to-csv%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is the script which reads LDIF from STDIN and output as 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
Click here for more
Thanks Bunch... Excellent script... it worked for me. I may have to add couple of more conditions for validation, but it worked like a champ. Regards Riz
– Riz
Jul 19 '18 at 18:29
add a comment |
This is the script which reads LDIF from STDIN and output as 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
Click here for more
Thanks Bunch... Excellent script... it worked for me. I may have to add couple of more conditions for validation, but it worked like a champ. Regards Riz
– Riz
Jul 19 '18 at 18:29
add a comment |
This is the script which reads LDIF from STDIN and output as 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
Click here for more
This is the script which reads LDIF from STDIN and output as 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
Click here for more
answered Jul 19 '18 at 4:15
Ujjwal SinghUjjwal Singh
72113
72113
Thanks Bunch... Excellent script... it worked for me. I may have to add couple of more conditions for validation, but it worked like a champ. Regards Riz
– Riz
Jul 19 '18 at 18:29
add a comment |
Thanks Bunch... Excellent script... it worked for me. I may have to add couple of more conditions for validation, but it worked like a champ. Regards Riz
– Riz
Jul 19 '18 at 18:29
Thanks Bunch... Excellent script... it worked for me. I may have to add couple of more conditions for validation, but it worked like a champ. Regards Riz
– Riz
Jul 19 '18 at 18:29
Thanks Bunch... Excellent script... it worked for me. I may have to add couple of more conditions for validation, but it worked like a champ. Regards Riz
– Riz
Jul 19 '18 at 18:29
add a comment |
I see some serious deficiencies in simple scripts like this:
- no proper handling of base64-encoded data which is used for non-ASCII chars or octet strings
- no proper handling of line-wrapping
- LDAP data model has multi-valued attributes
If you don't want to fix this yourself after reading through RFC 2849 I'd recommend to implement a short Python script using the python-ldap sub-module ldif and built-in csv module.
add a comment |
I see some serious deficiencies in simple scripts like this:
- no proper handling of base64-encoded data which is used for non-ASCII chars or octet strings
- no proper handling of line-wrapping
- LDAP data model has multi-valued attributes
If you don't want to fix this yourself after reading through RFC 2849 I'd recommend to implement a short Python script using the python-ldap sub-module ldif and built-in csv module.
add a comment |
I see some serious deficiencies in simple scripts like this:
- no proper handling of base64-encoded data which is used for non-ASCII chars or octet strings
- no proper handling of line-wrapping
- LDAP data model has multi-valued attributes
If you don't want to fix this yourself after reading through RFC 2849 I'd recommend to implement a short Python script using the python-ldap sub-module ldif and built-in csv module.
I see some serious deficiencies in simple scripts like this:
- no proper handling of base64-encoded data which is used for non-ASCII chars or octet strings
- no proper handling of line-wrapping
- LDAP data model has multi-valued attributes
If you don't want to fix this yourself after reading through RFC 2849 I'd recommend to implement a short Python script using the python-ldap sub-module ldif and built-in csv module.
answered Jul 20 '18 at 20:18
Michael StröderMichael Ströder
2547
2547
add a comment |
add a comment |
with Miller (http://johnkerl.org/miller/doc) and sed is very short and easy
sed 's/://g' input.txt | mlr --x2c cut -x -f CC
gives you
AA,BB,DD
User11_Value1,User11_Value2,User11 Space Value4
User22_Value1,User22_Value2,User22 Space Value4
Whit sed I remove the :
to obtain one of the native Miller input format (XTAB), than I convert XTAB to CSV with --x2c
and at the end I remove CC
field with cut.
add a comment |
with Miller (http://johnkerl.org/miller/doc) and sed is very short and easy
sed 's/://g' input.txt | mlr --x2c cut -x -f CC
gives you
AA,BB,DD
User11_Value1,User11_Value2,User11 Space Value4
User22_Value1,User22_Value2,User22 Space Value4
Whit sed I remove the :
to obtain one of the native Miller input format (XTAB), than I convert XTAB to CSV with --x2c
and at the end I remove CC
field with cut.
add a comment |
with Miller (http://johnkerl.org/miller/doc) and sed is very short and easy
sed 's/://g' input.txt | mlr --x2c cut -x -f CC
gives you
AA,BB,DD
User11_Value1,User11_Value2,User11 Space Value4
User22_Value1,User22_Value2,User22 Space Value4
Whit sed I remove the :
to obtain one of the native Miller input format (XTAB), than I convert XTAB to CSV with --x2c
and at the end I remove CC
field with cut.
with Miller (http://johnkerl.org/miller/doc) and sed is very short and easy
sed 's/://g' input.txt | mlr --x2c cut -x -f CC
gives you
AA,BB,DD
User11_Value1,User11_Value2,User11 Space Value4
User22_Value1,User22_Value2,User22 Space Value4
Whit sed I remove the :
to obtain one of the native Miller input format (XTAB), than I convert XTAB to CSV with --x2c
and at the end I remove CC
field with cut.
edited 2 days ago
answered 2 days ago
aborrusoaborruso
1619
1619
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f457091%2fconvert-data-from-ldif-file-to-csv%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown