Convert data from LDIF file to CSV

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















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









share|improve this question




























    0















    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









    share|improve this question


























      0












      0








      0








      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









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 0:17









      Rui F Ribeiro

      39.6k1479132




      39.6k1479132










      asked Jul 18 '18 at 21:23









      RizRiz

      1




      1




















          3 Answers
          3






          active

          oldest

          votes


















          0














          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






          share|improve this answer























          • 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



















          0














          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.






          share|improve this answer






























            0
















            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.






            share|improve this answer
























              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
              );



              );













              draft saved

              draft discarded


















              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









              0














              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






              share|improve this answer























              • 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
















              0














              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






              share|improve this answer























              • 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














              0












              0








              0







              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






              share|improve this answer













              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







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jul 19 '18 at 4:15









              Ujjwal SinghUjjwal Singh

              70113




              70113












              • 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






              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














              0














              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.






              share|improve this answer



























                0














                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.






                share|improve this answer

























                  0












                  0








                  0







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 20 '18 at 20:18









                  Michael StröderMichael Ströder

                  2547




                  2547





















                      0
















                      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.






                      share|improve this answer





























                        0
















                        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.






                        share|improve this answer



























                          0












                          0








                          0









                          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.






                          share|improve this answer

















                          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.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 6 at 18:08

























                          answered Jan 6 at 17:55









                          aborrusoaborruso

                          20619




                          20619



























                              draft saved

                              draft discarded
















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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






                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?