Issue
I'm trying to replace a block of text from file_to_change.xml with the entire content of changes.xml, which is a tag of the main file with a minor change.
This is the content of file_to_change.xml:
<clients>
<client>
<name>Bob</name>
<age>18</age>
</client>
<client>
<name>Alice</name>
<age>12</age>
</client>
<client>
<name>Carlos</name>
<age>28</age>
</client>
</clients>
And this is the content of changes.xml:
<client>
<name>Carlita</name>
<age>17</age>
</client>
And this is the content of an auxiliary file that contains the line i want to change:
<client>
<name>Carlos</name>
<age>28</age>
</client>
The great difference between the solution i'm looking for from the one explained here is that the files are not entirely equal. It is also important to say that the environment i'm working at do not allow me to use external tools, and for that reason i'm trying to find a "core" solution, using sed, awk, etc.
This is the command i was trying to use:
sed -i -e '1r changes.xml' -e '1,/r aux.xml/d' file_to_change.xml
I'm expecting that the file_to_change.xml look like this:
<clients>
<client>
<name>Bob</name>
<age>18</age>
</client>
<client>
<name>Alice</name>
<age>12</age>
</client>
<client>
<name>Carlita</name>
<age>17</age>
</client>
</clients>
Solution
Using pure bash?
#!/bin/bash
file=$( < file_to_change.xml )
search=$( < aux.xml )
replacement=$( < changes.xml )
printf '%s\n' "${file/"$search"/$replacement}"
<clients>
<client>
<name>Bob</name>
<age>18</age>
</client>
<client>
<name>Alice</name>
<age>12</age>
</client>
<client>
<name>Carlita</name>
<age>17</age>
</client>
</clients>
Answered By - Fravadona Answer Checked By - Clifford M. (WPSolving Volunteer)