How to delete tags from xml

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












1















I have the following XML file:



<?xml version="1.0" encoding="utf-8"?>
<deviceID>IO238470374109730497</devicdeID>
<KeyValuePairs>
<KeyValuePair>
<BASE-URL>2</BASE-URL>
<Key>2</Key>
<Value>2</Value>
</KeyValuePair>
<KeyValuePair>
<BASE-URL>3</BASE-URL>
<Key>3</Key>
<Value>3</Value>
</KeyValuePair>
</KeyValuePairs>


I want to delete only this section:



<KeyValuePair>
<BASE-URL>3</BASE-URL>
<Key>3</Key>
<Value>3</Value>
</KeyValuePair>


What I want to achieve is to delete what is inside and including <KeyValuePair> only for these specific <BASE-URL>, <Key> and <Value>. The <KeyValuePair> tuple is always unique and won't be repeated, but it can be one or more.



How can I do that using bash?










share|improve this question




























    1















    I have the following XML file:



    <?xml version="1.0" encoding="utf-8"?>
    <deviceID>IO238470374109730497</devicdeID>
    <KeyValuePairs>
    <KeyValuePair>
    <BASE-URL>2</BASE-URL>
    <Key>2</Key>
    <Value>2</Value>
    </KeyValuePair>
    <KeyValuePair>
    <BASE-URL>3</BASE-URL>
    <Key>3</Key>
    <Value>3</Value>
    </KeyValuePair>
    </KeyValuePairs>


    I want to delete only this section:



    <KeyValuePair>
    <BASE-URL>3</BASE-URL>
    <Key>3</Key>
    <Value>3</Value>
    </KeyValuePair>


    What I want to achieve is to delete what is inside and including <KeyValuePair> only for these specific <BASE-URL>, <Key> and <Value>. The <KeyValuePair> tuple is always unique and won't be repeated, but it can be one or more.



    How can I do that using bash?










    share|improve this question


























      1












      1








      1








      I have the following XML file:



      <?xml version="1.0" encoding="utf-8"?>
      <deviceID>IO238470374109730497</devicdeID>
      <KeyValuePairs>
      <KeyValuePair>
      <BASE-URL>2</BASE-URL>
      <Key>2</Key>
      <Value>2</Value>
      </KeyValuePair>
      <KeyValuePair>
      <BASE-URL>3</BASE-URL>
      <Key>3</Key>
      <Value>3</Value>
      </KeyValuePair>
      </KeyValuePairs>


      I want to delete only this section:



      <KeyValuePair>
      <BASE-URL>3</BASE-URL>
      <Key>3</Key>
      <Value>3</Value>
      </KeyValuePair>


      What I want to achieve is to delete what is inside and including <KeyValuePair> only for these specific <BASE-URL>, <Key> and <Value>. The <KeyValuePair> tuple is always unique and won't be repeated, but it can be one or more.



      How can I do that using bash?










      share|improve this question
















      I have the following XML file:



      <?xml version="1.0" encoding="utf-8"?>
      <deviceID>IO238470374109730497</devicdeID>
      <KeyValuePairs>
      <KeyValuePair>
      <BASE-URL>2</BASE-URL>
      <Key>2</Key>
      <Value>2</Value>
      </KeyValuePair>
      <KeyValuePair>
      <BASE-URL>3</BASE-URL>
      <Key>3</Key>
      <Value>3</Value>
      </KeyValuePair>
      </KeyValuePairs>


      I want to delete only this section:



      <KeyValuePair>
      <BASE-URL>3</BASE-URL>
      <Key>3</Key>
      <Value>3</Value>
      </KeyValuePair>


      What I want to achieve is to delete what is inside and including <KeyValuePair> only for these specific <BASE-URL>, <Key> and <Value>. The <KeyValuePair> tuple is always unique and won't be repeated, but it can be one or more.



      How can I do that using bash?







      linux text-processing xml






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 12 at 18:19









      roaima

      45.3k757123




      45.3k757123










      asked Feb 12 at 14:01









      Mustafa W AlaniMustafa W Alani

      103




      103




















          1 Answer
          1






          active

          oldest

          votes


















          4














          Your example isn't a valid XML file. I've altered it slightly by adding a <root/> element surrounding the document (and fixing your typo in the closing </deviceID> marker):



          <?xml version="1.0" encoding="utf-8"?>
          <root>
          <deviceID>IO238470374109730497</deviceID>
          <KeyValuePairs>
          <KeyValuePair>
          <BASE-URL>2</BASE-URL>
          <Key>2</Key>
          <Value>2</Value>
          </KeyValuePair>
          <KeyValuePair>
          <BASE-URL>3</BASE-URL>
          <Key>3</Key>
          <Value>3</Value>
          </KeyValuePair>
          </KeyValuePairs>
          </root>


          Now it's possible to use an XML editing tool to edit your XML. This XPath expression tells xmlstarlet to delete the XML element <KeyValuePair/> (and its content) where <BASE-URL/> has a text value 3.



          xmlstarlet ed -d '//KeyValuePair[BASE-URL/text()="3"]' /tmp/500185.xml


          And the output is



          <?xml version="1.0" encoding="utf-8"?>
          <root>
          <deviceID>IO238470374109730497</deviceID>
          <KeyValuePairs>
          <KeyValuePair>
          <BASE-URL>2</BASE-URL>
          <Key>2</Key>
          <Value>2</Value>
          </KeyValuePair>
          </KeyValuePairs>
          </root>





          share|improve this answer























          • Thank you very much it works! How about doing this as a shell script? I also want to make another script to read the values in <Value> and call a curl command to their <BASE-URL>

            – Mustafa W Alani
            Feb 12 at 19:37











          • xmlstarlet is a command you can call from a shell script, just like sed or awk or grep. There are lots of examples for xmlstarlet here on Unix & Linux and also on the other Stack Exchange sites. (I've written some of them.)

            – roaima
            Feb 12 at 19:49











          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%2f500185%2fhow-to-delete-tags-from-xml%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          Your example isn't a valid XML file. I've altered it slightly by adding a <root/> element surrounding the document (and fixing your typo in the closing </deviceID> marker):



          <?xml version="1.0" encoding="utf-8"?>
          <root>
          <deviceID>IO238470374109730497</deviceID>
          <KeyValuePairs>
          <KeyValuePair>
          <BASE-URL>2</BASE-URL>
          <Key>2</Key>
          <Value>2</Value>
          </KeyValuePair>
          <KeyValuePair>
          <BASE-URL>3</BASE-URL>
          <Key>3</Key>
          <Value>3</Value>
          </KeyValuePair>
          </KeyValuePairs>
          </root>


          Now it's possible to use an XML editing tool to edit your XML. This XPath expression tells xmlstarlet to delete the XML element <KeyValuePair/> (and its content) where <BASE-URL/> has a text value 3.



          xmlstarlet ed -d '//KeyValuePair[BASE-URL/text()="3"]' /tmp/500185.xml


          And the output is



          <?xml version="1.0" encoding="utf-8"?>
          <root>
          <deviceID>IO238470374109730497</deviceID>
          <KeyValuePairs>
          <KeyValuePair>
          <BASE-URL>2</BASE-URL>
          <Key>2</Key>
          <Value>2</Value>
          </KeyValuePair>
          </KeyValuePairs>
          </root>





          share|improve this answer























          • Thank you very much it works! How about doing this as a shell script? I also want to make another script to read the values in <Value> and call a curl command to their <BASE-URL>

            – Mustafa W Alani
            Feb 12 at 19:37











          • xmlstarlet is a command you can call from a shell script, just like sed or awk or grep. There are lots of examples for xmlstarlet here on Unix & Linux and also on the other Stack Exchange sites. (I've written some of them.)

            – roaima
            Feb 12 at 19:49
















          4














          Your example isn't a valid XML file. I've altered it slightly by adding a <root/> element surrounding the document (and fixing your typo in the closing </deviceID> marker):



          <?xml version="1.0" encoding="utf-8"?>
          <root>
          <deviceID>IO238470374109730497</deviceID>
          <KeyValuePairs>
          <KeyValuePair>
          <BASE-URL>2</BASE-URL>
          <Key>2</Key>
          <Value>2</Value>
          </KeyValuePair>
          <KeyValuePair>
          <BASE-URL>3</BASE-URL>
          <Key>3</Key>
          <Value>3</Value>
          </KeyValuePair>
          </KeyValuePairs>
          </root>


          Now it's possible to use an XML editing tool to edit your XML. This XPath expression tells xmlstarlet to delete the XML element <KeyValuePair/> (and its content) where <BASE-URL/> has a text value 3.



          xmlstarlet ed -d '//KeyValuePair[BASE-URL/text()="3"]' /tmp/500185.xml


          And the output is



          <?xml version="1.0" encoding="utf-8"?>
          <root>
          <deviceID>IO238470374109730497</deviceID>
          <KeyValuePairs>
          <KeyValuePair>
          <BASE-URL>2</BASE-URL>
          <Key>2</Key>
          <Value>2</Value>
          </KeyValuePair>
          </KeyValuePairs>
          </root>





          share|improve this answer























          • Thank you very much it works! How about doing this as a shell script? I also want to make another script to read the values in <Value> and call a curl command to their <BASE-URL>

            – Mustafa W Alani
            Feb 12 at 19:37











          • xmlstarlet is a command you can call from a shell script, just like sed or awk or grep. There are lots of examples for xmlstarlet here on Unix & Linux and also on the other Stack Exchange sites. (I've written some of them.)

            – roaima
            Feb 12 at 19:49














          4












          4








          4







          Your example isn't a valid XML file. I've altered it slightly by adding a <root/> element surrounding the document (and fixing your typo in the closing </deviceID> marker):



          <?xml version="1.0" encoding="utf-8"?>
          <root>
          <deviceID>IO238470374109730497</deviceID>
          <KeyValuePairs>
          <KeyValuePair>
          <BASE-URL>2</BASE-URL>
          <Key>2</Key>
          <Value>2</Value>
          </KeyValuePair>
          <KeyValuePair>
          <BASE-URL>3</BASE-URL>
          <Key>3</Key>
          <Value>3</Value>
          </KeyValuePair>
          </KeyValuePairs>
          </root>


          Now it's possible to use an XML editing tool to edit your XML. This XPath expression tells xmlstarlet to delete the XML element <KeyValuePair/> (and its content) where <BASE-URL/> has a text value 3.



          xmlstarlet ed -d '//KeyValuePair[BASE-URL/text()="3"]' /tmp/500185.xml


          And the output is



          <?xml version="1.0" encoding="utf-8"?>
          <root>
          <deviceID>IO238470374109730497</deviceID>
          <KeyValuePairs>
          <KeyValuePair>
          <BASE-URL>2</BASE-URL>
          <Key>2</Key>
          <Value>2</Value>
          </KeyValuePair>
          </KeyValuePairs>
          </root>





          share|improve this answer













          Your example isn't a valid XML file. I've altered it slightly by adding a <root/> element surrounding the document (and fixing your typo in the closing </deviceID> marker):



          <?xml version="1.0" encoding="utf-8"?>
          <root>
          <deviceID>IO238470374109730497</deviceID>
          <KeyValuePairs>
          <KeyValuePair>
          <BASE-URL>2</BASE-URL>
          <Key>2</Key>
          <Value>2</Value>
          </KeyValuePair>
          <KeyValuePair>
          <BASE-URL>3</BASE-URL>
          <Key>3</Key>
          <Value>3</Value>
          </KeyValuePair>
          </KeyValuePairs>
          </root>


          Now it's possible to use an XML editing tool to edit your XML. This XPath expression tells xmlstarlet to delete the XML element <KeyValuePair/> (and its content) where <BASE-URL/> has a text value 3.



          xmlstarlet ed -d '//KeyValuePair[BASE-URL/text()="3"]' /tmp/500185.xml


          And the output is



          <?xml version="1.0" encoding="utf-8"?>
          <root>
          <deviceID>IO238470374109730497</deviceID>
          <KeyValuePairs>
          <KeyValuePair>
          <BASE-URL>2</BASE-URL>
          <Key>2</Key>
          <Value>2</Value>
          </KeyValuePair>
          </KeyValuePairs>
          </root>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 12 at 15:07









          roaimaroaima

          45.3k757123




          45.3k757123












          • Thank you very much it works! How about doing this as a shell script? I also want to make another script to read the values in <Value> and call a curl command to their <BASE-URL>

            – Mustafa W Alani
            Feb 12 at 19:37











          • xmlstarlet is a command you can call from a shell script, just like sed or awk or grep. There are lots of examples for xmlstarlet here on Unix & Linux and also on the other Stack Exchange sites. (I've written some of them.)

            – roaima
            Feb 12 at 19:49


















          • Thank you very much it works! How about doing this as a shell script? I also want to make another script to read the values in <Value> and call a curl command to their <BASE-URL>

            – Mustafa W Alani
            Feb 12 at 19:37











          • xmlstarlet is a command you can call from a shell script, just like sed or awk or grep. There are lots of examples for xmlstarlet here on Unix & Linux and also on the other Stack Exchange sites. (I've written some of them.)

            – roaima
            Feb 12 at 19:49

















          Thank you very much it works! How about doing this as a shell script? I also want to make another script to read the values in <Value> and call a curl command to their <BASE-URL>

          – Mustafa W Alani
          Feb 12 at 19:37





          Thank you very much it works! How about doing this as a shell script? I also want to make another script to read the values in <Value> and call a curl command to their <BASE-URL>

          – Mustafa W Alani
          Feb 12 at 19:37













          xmlstarlet is a command you can call from a shell script, just like sed or awk or grep. There are lots of examples for xmlstarlet here on Unix & Linux and also on the other Stack Exchange sites. (I've written some of them.)

          – roaima
          Feb 12 at 19:49






          xmlstarlet is a command you can call from a shell script, just like sed or awk or grep. There are lots of examples for xmlstarlet here on Unix & Linux and also on the other Stack Exchange sites. (I've written some of them.)

          – roaima
          Feb 12 at 19:49


















          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%2f500185%2fhow-to-delete-tags-from-xml%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?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay