Replace an XML attribute's value with the value of a shell variable

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











up vote
0
down vote

favorite












<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
<POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
<REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8"
DATABASETYPE="Oracle">
<FOLDER NAME="ABC" GROUP="" OWNER="Administrator" SHARED="SHARED"
DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-
def2bb8424ef">


I have the above sample data. In which file will have FOLDER NAME with some value. I have to replace it with some other value which is a variable
I want to replace FOLDER NAME="ABC" with DEF



 sed -i "s/<FOLDER NAME="*"/<FOLDER NAME="$FLDR"/g" Gather.XML


Above sed commands doesn't through any error but it is not replacing.







share|improve this question

























    up vote
    0
    down vote

    favorite












    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
    <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
    <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8"
    DATABASETYPE="Oracle">
    <FOLDER NAME="ABC" GROUP="" OWNER="Administrator" SHARED="SHARED"
    DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-
    def2bb8424ef">


    I have the above sample data. In which file will have FOLDER NAME with some value. I have to replace it with some other value which is a variable
    I want to replace FOLDER NAME="ABC" with DEF



     sed -i "s/<FOLDER NAME="*"/<FOLDER NAME="$FLDR"/g" Gather.XML


    Above sed commands doesn't through any error but it is not replacing.







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
      <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
      <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8"
      DATABASETYPE="Oracle">
      <FOLDER NAME="ABC" GROUP="" OWNER="Administrator" SHARED="SHARED"
      DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-
      def2bb8424ef">


      I have the above sample data. In which file will have FOLDER NAME with some value. I have to replace it with some other value which is a variable
      I want to replace FOLDER NAME="ABC" with DEF



       sed -i "s/<FOLDER NAME="*"/<FOLDER NAME="$FLDR"/g" Gather.XML


      Above sed commands doesn't through any error but it is not replacing.







      share|improve this question













      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
      <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
      <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8"
      DATABASETYPE="Oracle">
      <FOLDER NAME="ABC" GROUP="" OWNER="Administrator" SHARED="SHARED"
      DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-
      def2bb8424ef">


      I have the above sample data. In which file will have FOLDER NAME with some value. I have to replace it with some other value which is a variable
      I want to replace FOLDER NAME="ABC" with DEF



       sed -i "s/<FOLDER NAME="*"/<FOLDER NAME="$FLDR"/g" Gather.XML


      Above sed commands doesn't through any error but it is not replacing.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 27 at 1:38









      Jeff Schaller

      30.8k846104




      30.8k846104









      asked Jun 26 at 7:22









      user_297020

      156




      156




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Try this,



          sed -i 's/<FOLDER NAME="[A-Z]*"/<FOLDER NAME="'$FLDR'"/g' Gather.XM





          share|improve this answer




























            up vote
            2
            down vote













            Assuming this is a well-formed XML document, using XMLStarlet:



            xmlstarlet ed -u '/POWERMART/REPOSITORY/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml


            This would locate the FOLDER node under /POWERMART/REPOSITORY whose NAME attribute is ABC and change its value to the value of the shell variable FLDR.



            Example:



            $ cat file.xml
            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
            <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
            <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8" DATABASETYPE="Oracle">
            <FOLDER NAME="ABC" GROUP="" OWNER="Administrator" SHARED="SHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-def2bb8424ef"/>
            </REPOSITORY>
            </POWERMART>

            $ FLDR='DEF'
            $ xmlstarlet ed -u '/POWERMART/REPOSITORY/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml
            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
            <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
            <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8" DATABASETYPE="Oracle">
            <FOLDER NAME="DEF" GROUP="" OWNER="Administrator" SHARED="SHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-def2bb8424ef"/>
            </REPOSITORY>
            </POWERMART>


            If you have to match a particular REPOSITORY's NAME, then e.g.



            xmlstarlet ed -u '/POWERMART/REPOSITORY[@NAME="PCREPO_BIDEV"]/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml





            share|improve this answer






























              up vote
              0
              down vote













              You are missing a "." in your sed expression to match all characters



               sed -i "s/<FOLDER NAME=".*"/<FOLDER NAME="$FLDR"/g" Gather.XML





              share|improve this answer




























                up vote
                0
                down vote













                It looks like you should do [^"]* instead of *:



                sed -i "s/<FOLDER NAME="["]*"/<FOLDER NAME="$FLDR"/g" Gather.XML


                Since you want to match all text till the first " there. * will match the literal * character, but you don't have that there, so the file's contents never match.






                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%2f451936%2freplace-an-xml-attributes-value-with-the-value-of-a-shell-variable%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  1
                  down vote



                  accepted










                  Try this,



                  sed -i 's/<FOLDER NAME="[A-Z]*"/<FOLDER NAME="'$FLDR'"/g' Gather.XM





                  share|improve this answer

























                    up vote
                    1
                    down vote



                    accepted










                    Try this,



                    sed -i 's/<FOLDER NAME="[A-Z]*"/<FOLDER NAME="'$FLDR'"/g' Gather.XM





                    share|improve this answer























                      up vote
                      1
                      down vote



                      accepted







                      up vote
                      1
                      down vote



                      accepted






                      Try this,



                      sed -i 's/<FOLDER NAME="[A-Z]*"/<FOLDER NAME="'$FLDR'"/g' Gather.XM





                      share|improve this answer













                      Try this,



                      sed -i 's/<FOLDER NAME="[A-Z]*"/<FOLDER NAME="'$FLDR'"/g' Gather.XM






                      share|improve this answer













                      share|improve this answer



                      share|improve this answer











                      answered Jun 26 at 7:58









                      SivaPrasath

                      3,88611737




                      3,88611737






















                          up vote
                          2
                          down vote













                          Assuming this is a well-formed XML document, using XMLStarlet:



                          xmlstarlet ed -u '/POWERMART/REPOSITORY/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml


                          This would locate the FOLDER node under /POWERMART/REPOSITORY whose NAME attribute is ABC and change its value to the value of the shell variable FLDR.



                          Example:



                          $ cat file.xml
                          <?xml version="1.0" encoding="UTF-8"?>
                          <!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
                          <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
                          <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8" DATABASETYPE="Oracle">
                          <FOLDER NAME="ABC" GROUP="" OWNER="Administrator" SHARED="SHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-def2bb8424ef"/>
                          </REPOSITORY>
                          </POWERMART>

                          $ FLDR='DEF'
                          $ xmlstarlet ed -u '/POWERMART/REPOSITORY/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml
                          <?xml version="1.0" encoding="UTF-8"?>
                          <!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
                          <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
                          <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8" DATABASETYPE="Oracle">
                          <FOLDER NAME="DEF" GROUP="" OWNER="Administrator" SHARED="SHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-def2bb8424ef"/>
                          </REPOSITORY>
                          </POWERMART>


                          If you have to match a particular REPOSITORY's NAME, then e.g.



                          xmlstarlet ed -u '/POWERMART/REPOSITORY[@NAME="PCREPO_BIDEV"]/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml





                          share|improve this answer



























                            up vote
                            2
                            down vote













                            Assuming this is a well-formed XML document, using XMLStarlet:



                            xmlstarlet ed -u '/POWERMART/REPOSITORY/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml


                            This would locate the FOLDER node under /POWERMART/REPOSITORY whose NAME attribute is ABC and change its value to the value of the shell variable FLDR.



                            Example:



                            $ cat file.xml
                            <?xml version="1.0" encoding="UTF-8"?>
                            <!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
                            <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
                            <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8" DATABASETYPE="Oracle">
                            <FOLDER NAME="ABC" GROUP="" OWNER="Administrator" SHARED="SHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-def2bb8424ef"/>
                            </REPOSITORY>
                            </POWERMART>

                            $ FLDR='DEF'
                            $ xmlstarlet ed -u '/POWERMART/REPOSITORY/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml
                            <?xml version="1.0" encoding="UTF-8"?>
                            <!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
                            <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
                            <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8" DATABASETYPE="Oracle">
                            <FOLDER NAME="DEF" GROUP="" OWNER="Administrator" SHARED="SHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-def2bb8424ef"/>
                            </REPOSITORY>
                            </POWERMART>


                            If you have to match a particular REPOSITORY's NAME, then e.g.



                            xmlstarlet ed -u '/POWERMART/REPOSITORY[@NAME="PCREPO_BIDEV"]/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml





                            share|improve this answer

























                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote









                              Assuming this is a well-formed XML document, using XMLStarlet:



                              xmlstarlet ed -u '/POWERMART/REPOSITORY/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml


                              This would locate the FOLDER node under /POWERMART/REPOSITORY whose NAME attribute is ABC and change its value to the value of the shell variable FLDR.



                              Example:



                              $ cat file.xml
                              <?xml version="1.0" encoding="UTF-8"?>
                              <!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
                              <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
                              <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8" DATABASETYPE="Oracle">
                              <FOLDER NAME="ABC" GROUP="" OWNER="Administrator" SHARED="SHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-def2bb8424ef"/>
                              </REPOSITORY>
                              </POWERMART>

                              $ FLDR='DEF'
                              $ xmlstarlet ed -u '/POWERMART/REPOSITORY/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml
                              <?xml version="1.0" encoding="UTF-8"?>
                              <!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
                              <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
                              <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8" DATABASETYPE="Oracle">
                              <FOLDER NAME="DEF" GROUP="" OWNER="Administrator" SHARED="SHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-def2bb8424ef"/>
                              </REPOSITORY>
                              </POWERMART>


                              If you have to match a particular REPOSITORY's NAME, then e.g.



                              xmlstarlet ed -u '/POWERMART/REPOSITORY[@NAME="PCREPO_BIDEV"]/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml





                              share|improve this answer















                              Assuming this is a well-formed XML document, using XMLStarlet:



                              xmlstarlet ed -u '/POWERMART/REPOSITORY/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml


                              This would locate the FOLDER node under /POWERMART/REPOSITORY whose NAME attribute is ABC and change its value to the value of the shell variable FLDR.



                              Example:



                              $ cat file.xml
                              <?xml version="1.0" encoding="UTF-8"?>
                              <!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
                              <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
                              <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8" DATABASETYPE="Oracle">
                              <FOLDER NAME="ABC" GROUP="" OWNER="Administrator" SHARED="SHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-def2bb8424ef"/>
                              </REPOSITORY>
                              </POWERMART>

                              $ FLDR='DEF'
                              $ xmlstarlet ed -u '/POWERMART/REPOSITORY/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml
                              <?xml version="1.0" encoding="UTF-8"?>
                              <!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
                              <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
                              <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8" DATABASETYPE="Oracle">
                              <FOLDER NAME="DEF" GROUP="" OWNER="Administrator" SHARED="SHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-def2bb8424ef"/>
                              </REPOSITORY>
                              </POWERMART>


                              If you have to match a particular REPOSITORY's NAME, then e.g.



                              xmlstarlet ed -u '/POWERMART/REPOSITORY[@NAME="PCREPO_BIDEV"]/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml






                              share|improve this answer















                              share|improve this answer



                              share|improve this answer








                              edited Jun 26 at 11:28


























                              answered Jun 26 at 10:10









                              Kusalananda

                              101k13199312




                              101k13199312




















                                  up vote
                                  0
                                  down vote













                                  You are missing a "." in your sed expression to match all characters



                                   sed -i "s/<FOLDER NAME=".*"/<FOLDER NAME="$FLDR"/g" Gather.XML





                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote













                                    You are missing a "." in your sed expression to match all characters



                                     sed -i "s/<FOLDER NAME=".*"/<FOLDER NAME="$FLDR"/g" Gather.XML





                                    share|improve this answer























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      You are missing a "." in your sed expression to match all characters



                                       sed -i "s/<FOLDER NAME=".*"/<FOLDER NAME="$FLDR"/g" Gather.XML





                                      share|improve this answer













                                      You are missing a "." in your sed expression to match all characters



                                       sed -i "s/<FOLDER NAME=".*"/<FOLDER NAME="$FLDR"/g" Gather.XML






                                      share|improve this answer













                                      share|improve this answer



                                      share|improve this answer











                                      answered Jun 26 at 7:26









                                      Arushix

                                      9968




                                      9968




















                                          up vote
                                          0
                                          down vote













                                          It looks like you should do [^"]* instead of *:



                                          sed -i "s/<FOLDER NAME="["]*"/<FOLDER NAME="$FLDR"/g" Gather.XML


                                          Since you want to match all text till the first " there. * will match the literal * character, but you don't have that there, so the file's contents never match.






                                          share|improve this answer

























                                            up vote
                                            0
                                            down vote













                                            It looks like you should do [^"]* instead of *:



                                            sed -i "s/<FOLDER NAME="["]*"/<FOLDER NAME="$FLDR"/g" Gather.XML


                                            Since you want to match all text till the first " there. * will match the literal * character, but you don't have that there, so the file's contents never match.






                                            share|improve this answer























                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              It looks like you should do [^"]* instead of *:



                                              sed -i "s/<FOLDER NAME="["]*"/<FOLDER NAME="$FLDR"/g" Gather.XML


                                              Since you want to match all text till the first " there. * will match the literal * character, but you don't have that there, so the file's contents never match.






                                              share|improve this answer













                                              It looks like you should do [^"]* instead of *:



                                              sed -i "s/<FOLDER NAME="["]*"/<FOLDER NAME="$FLDR"/g" Gather.XML


                                              Since you want to match all text till the first " there. * will match the literal * character, but you don't have that there, so the file's contents never match.







                                              share|improve this answer













                                              share|improve this answer



                                              share|improve this answer











                                              answered Jun 26 at 7:27









                                              Olorin

                                              1,15711




                                              1,15711






















                                                   

                                                  draft saved


                                                  draft discarded


























                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451936%2freplace-an-xml-attributes-value-with-the-value-of-a-shell-variable%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