How to delete tags from xml
Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
add a comment |
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
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
linux text-processing xml
edited Feb 12 at 18:19
roaima
45.3k757123
45.3k757123
asked Feb 12 at 14:01
Mustafa W AlaniMustafa W Alani
103
103
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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>
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 likesed
orawk
orgrep
. There are lots of examples forxmlstarlet
here on Unix & Linux and also on the other Stack Exchange sites. (I've written some of them.)
– roaima
Feb 12 at 19:49
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%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
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>
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 likesed
orawk
orgrep
. There are lots of examples forxmlstarlet
here on Unix & Linux and also on the other Stack Exchange sites. (I've written some of them.)
– roaima
Feb 12 at 19:49
add a comment |
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>
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 likesed
orawk
orgrep
. There are lots of examples forxmlstarlet
here on Unix & Linux and also on the other Stack Exchange sites. (I've written some of them.)
– roaima
Feb 12 at 19:49
add a comment |
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>
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>
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 likesed
orawk
orgrep
. There are lots of examples forxmlstarlet
here on Unix & Linux and also on the other Stack Exchange sites. (I've written some of them.)
– roaima
Feb 12 at 19:49
add a comment |
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 likesed
orawk
orgrep
. There are lots of examples forxmlstarlet
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
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.
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%2f500185%2fhow-to-delete-tags-from-xml%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