Matching substring within LDAP DN using Awk

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











up vote
0
down vote

favorite












I'm looking to parse the output of an LDAP query. I want to retrieve a specific OU value within the DN that I am printing



dn: ObjectName=Value,ou=12,ou=Users,dc=example,dc=com


I am retrieving 3 total attributes values, but I only want the ou=[d]1,2 to be printed as part of the DN that I am printing. Here is an example of my code that gives the full DN:



<LDAP SEARCH QUERY> | awk -F': ' '/dn: /dn=$2/^Attribute1: /Attribute1=$2/^Attribute2: /print dn","Attribute1","$2'


I'm not sure how to parse the ou=12 in the example. I have tried several different but have not succeeded. I would like to use Awk to do this as this will be ported to several systems and perl/python isn't an option in all cases. I suppose it's like using grep -o but using Awk instead.



Here is the desired output:



12,Attribute1,Attribute2






share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm looking to parse the output of an LDAP query. I want to retrieve a specific OU value within the DN that I am printing



    dn: ObjectName=Value,ou=12,ou=Users,dc=example,dc=com


    I am retrieving 3 total attributes values, but I only want the ou=[d]1,2 to be printed as part of the DN that I am printing. Here is an example of my code that gives the full DN:



    <LDAP SEARCH QUERY> | awk -F': ' '/dn: /dn=$2/^Attribute1: /Attribute1=$2/^Attribute2: /print dn","Attribute1","$2'


    I'm not sure how to parse the ou=12 in the example. I have tried several different but have not succeeded. I would like to use Awk to do this as this will be ported to several systems and perl/python isn't an option in all cases. I suppose it's like using grep -o but using Awk instead.



    Here is the desired output:



    12,Attribute1,Attribute2






    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm looking to parse the output of an LDAP query. I want to retrieve a specific OU value within the DN that I am printing



      dn: ObjectName=Value,ou=12,ou=Users,dc=example,dc=com


      I am retrieving 3 total attributes values, but I only want the ou=[d]1,2 to be printed as part of the DN that I am printing. Here is an example of my code that gives the full DN:



      <LDAP SEARCH QUERY> | awk -F': ' '/dn: /dn=$2/^Attribute1: /Attribute1=$2/^Attribute2: /print dn","Attribute1","$2'


      I'm not sure how to parse the ou=12 in the example. I have tried several different but have not succeeded. I would like to use Awk to do this as this will be ported to several systems and perl/python isn't an option in all cases. I suppose it's like using grep -o but using Awk instead.



      Here is the desired output:



      12,Attribute1,Attribute2






      share|improve this question














      I'm looking to parse the output of an LDAP query. I want to retrieve a specific OU value within the DN that I am printing



      dn: ObjectName=Value,ou=12,ou=Users,dc=example,dc=com


      I am retrieving 3 total attributes values, but I only want the ou=[d]1,2 to be printed as part of the DN that I am printing. Here is an example of my code that gives the full DN:



      <LDAP SEARCH QUERY> | awk -F': ' '/dn: /dn=$2/^Attribute1: /Attribute1=$2/^Attribute2: /print dn","Attribute1","$2'


      I'm not sure how to parse the ou=12 in the example. I have tried several different but have not succeeded. I would like to use Awk to do this as this will be ported to several systems and perl/python isn't an option in all cases. I suppose it's like using grep -o but using Awk instead.



      Here is the desired output:



      12,Attribute1,Attribute2








      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 10 at 19:02









      Jeff Schaller

      31.1k846105




      31.1k846105










      asked Apr 10 at 17:12









      drfunkenstein

      2516




      2516




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          If you use only basic awk you can split the found dn variable value into parts separated by the string ",ou=" (assuming ou= is not first), then either look for a number (or take the second item of the split if you know that is always that field):



          n = split(dn,x,",ou=")
          for(i=1;i<=n;i++)if(x[i]+0==x[i])v = x[i]
          # or just do: v = x[2]


          For your example input, you would get in array x at index 1, 2, and 3, the values:



          ObjectName=Value
          12
          Users,dc=example,dc=com


          We test for a number by adding 0 to the string. awk converts the string to a number (0 if it is not a number). If the result is the same as the original string, we have a simple number.



          Alternatively, if you have gnu awk you can use gensub to match for the pattern and capture with () the number part, replacing the whole dn value with it:



          v = gensub(".*,ou=([0-9]1,2),.*","\1",1,dn)





          share|improve this answer



























            up vote
            0
            down vote



            accepted










            Here's what I ended up doing:



            awk -F': ' '/^dn: /split($2, ou, "," seps)/^Attribute1/Attribute1=$2/^Attribute2/print ou[2]","$2","Attribute1'


            The "ou=" is still there, but I am able to easily modify my script to accommodate.



            ou=12,Attribute1,Attribute2





            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',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              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%2f436826%2fmatching-substring-within-ldap-dn-using-awk%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote













              If you use only basic awk you can split the found dn variable value into parts separated by the string ",ou=" (assuming ou= is not first), then either look for a number (or take the second item of the split if you know that is always that field):



              n = split(dn,x,",ou=")
              for(i=1;i<=n;i++)if(x[i]+0==x[i])v = x[i]
              # or just do: v = x[2]


              For your example input, you would get in array x at index 1, 2, and 3, the values:



              ObjectName=Value
              12
              Users,dc=example,dc=com


              We test for a number by adding 0 to the string. awk converts the string to a number (0 if it is not a number). If the result is the same as the original string, we have a simple number.



              Alternatively, if you have gnu awk you can use gensub to match for the pattern and capture with () the number part, replacing the whole dn value with it:



              v = gensub(".*,ou=([0-9]1,2),.*","\1",1,dn)





              share|improve this answer
























                up vote
                1
                down vote













                If you use only basic awk you can split the found dn variable value into parts separated by the string ",ou=" (assuming ou= is not first), then either look for a number (or take the second item of the split if you know that is always that field):



                n = split(dn,x,",ou=")
                for(i=1;i<=n;i++)if(x[i]+0==x[i])v = x[i]
                # or just do: v = x[2]


                For your example input, you would get in array x at index 1, 2, and 3, the values:



                ObjectName=Value
                12
                Users,dc=example,dc=com


                We test for a number by adding 0 to the string. awk converts the string to a number (0 if it is not a number). If the result is the same as the original string, we have a simple number.



                Alternatively, if you have gnu awk you can use gensub to match for the pattern and capture with () the number part, replacing the whole dn value with it:



                v = gensub(".*,ou=([0-9]1,2),.*","\1",1,dn)





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  If you use only basic awk you can split the found dn variable value into parts separated by the string ",ou=" (assuming ou= is not first), then either look for a number (or take the second item of the split if you know that is always that field):



                  n = split(dn,x,",ou=")
                  for(i=1;i<=n;i++)if(x[i]+0==x[i])v = x[i]
                  # or just do: v = x[2]


                  For your example input, you would get in array x at index 1, 2, and 3, the values:



                  ObjectName=Value
                  12
                  Users,dc=example,dc=com


                  We test for a number by adding 0 to the string. awk converts the string to a number (0 if it is not a number). If the result is the same as the original string, we have a simple number.



                  Alternatively, if you have gnu awk you can use gensub to match for the pattern and capture with () the number part, replacing the whole dn value with it:



                  v = gensub(".*,ou=([0-9]1,2),.*","\1",1,dn)





                  share|improve this answer












                  If you use only basic awk you can split the found dn variable value into parts separated by the string ",ou=" (assuming ou= is not first), then either look for a number (or take the second item of the split if you know that is always that field):



                  n = split(dn,x,",ou=")
                  for(i=1;i<=n;i++)if(x[i]+0==x[i])v = x[i]
                  # or just do: v = x[2]


                  For your example input, you would get in array x at index 1, 2, and 3, the values:



                  ObjectName=Value
                  12
                  Users,dc=example,dc=com


                  We test for a number by adding 0 to the string. awk converts the string to a number (0 if it is not a number). If the result is the same as the original string, we have a simple number.



                  Alternatively, if you have gnu awk you can use gensub to match for the pattern and capture with () the number part, replacing the whole dn value with it:



                  v = gensub(".*,ou=([0-9]1,2),.*","\1",1,dn)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 11 at 9:55









                  meuh

                  29.3k11649




                  29.3k11649






















                      up vote
                      0
                      down vote



                      accepted










                      Here's what I ended up doing:



                      awk -F': ' '/^dn: /split($2, ou, "," seps)/^Attribute1/Attribute1=$2/^Attribute2/print ou[2]","$2","Attribute1'


                      The "ou=" is still there, but I am able to easily modify my script to accommodate.



                      ou=12,Attribute1,Attribute2





                      share|improve this answer
























                        up vote
                        0
                        down vote



                        accepted










                        Here's what I ended up doing:



                        awk -F': ' '/^dn: /split($2, ou, "," seps)/^Attribute1/Attribute1=$2/^Attribute2/print ou[2]","$2","Attribute1'


                        The "ou=" is still there, but I am able to easily modify my script to accommodate.



                        ou=12,Attribute1,Attribute2





                        share|improve this answer






















                          up vote
                          0
                          down vote



                          accepted







                          up vote
                          0
                          down vote



                          accepted






                          Here's what I ended up doing:



                          awk -F': ' '/^dn: /split($2, ou, "," seps)/^Attribute1/Attribute1=$2/^Attribute2/print ou[2]","$2","Attribute1'


                          The "ou=" is still there, but I am able to easily modify my script to accommodate.



                          ou=12,Attribute1,Attribute2





                          share|improve this answer












                          Here's what I ended up doing:



                          awk -F': ' '/^dn: /split($2, ou, "," seps)/^Attribute1/Attribute1=$2/^Attribute2/print ou[2]","$2","Attribute1'


                          The "ou=" is still there, but I am able to easily modify my script to accommodate.



                          ou=12,Attribute1,Attribute2






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 12 at 15:11









                          drfunkenstein

                          2516




                          2516






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f436826%2fmatching-substring-within-ldap-dn-using-awk%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

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

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay