Score:0

How to use "sed" for replacing xml tag in multiple lines xml content on bash

bw flag

I want to use sed to replace the XML tag on bash.

Here is the example xml content for testing:

<xml-content>
<validation>
<timeout>2880</timeout>
<subject>example</subject>
<required>true</required>
</validation>
</xml-content>

I want to replace the tag validation:

<validation>
<timeout>2880</timeout>
<subject>example</subject>
<required>true</required>
</validation>

with the new tag other-tag:

<other-tag>
<hello/>
<more>false</more>
</other-tag>

Here is the final expected result:

<xml-content>
<other-tag>
<hello/>
<more>false</more>
</other-tag>
</xml-content>

How to do it with a single command line sed?

Score:2
jp flag

With sed you can select all lines in a file between two patterns(including the two lines containing PAT1 and PAT2) and replace them like so:

sed '/PAT1/,/PAT2/c \REPLACEMENT' file

REPLACEMENT is text, which has each embedded newline preceded by a backslash.

Notice that if PAT1 or PAT2 contain foreword slashes / then either each one of them must be escaped with a backslash \/ or the delimiter must be changed to something other than / and in which case you need to escape the first of each delimiter sequence when the c \ command or any other sed command is used e.g. you can change the delimiter to _ like so:

sed '\_PAT1_,\_PAT2_c \REPLACEMENT' file

So you can do:

sed '/<validation>/,/<\/validation>/c \<other-tag>\n<hello/>\n<more>false</more>\n</other-tag>' file

to edit this:

<xml-content>
<validation>
<timeout>2880</timeout>
<subject>example</subject>
<required>true</required>
</validation>
</xml-content>

to this:

<xml-content>
<other-tag>
<hello/>
<more>false</more>
</other-tag>
</xml-content>

Notice that the seds option -i is needed if you want to edit the original file in place ... Test without it first then use it with a backup suffix e.g. -i.back to save a backup copy of the original file with .back extension i.e. file.back like so:

sed -i.back '/PAT1/,/PAT2/c \REPLACEMENT' file

Notice as well that there are tools made especially to edit XML files that you can use to update,delete or insert XML elements ... See How do I replace multiple fields in multiple XML files? as an example ... Those tools however might require a rather more complex command script than what is required forsed in some use cases like yours ... I understand why you asked for a sed solution and would myself prefer sed as well over specialized XML tools for such task ... Plus sed would be more portable of course.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.