Search Keys and Replace Values in XML
<p>I have an xml file which looks like below</p>

<pre><code>&lt;name&gt;abcdefg&lt;/name&gt;
&lt;value&gt;123456&lt;/value&gt;
</code></pre>

<p>I am trying to write a script using sed to search for the tag "abcdefg" and then <strong>replace the corresponding value</strong> "123456" but unfortunately I am not able to find a logic to achieve above.
 Need help!</p>

<p>Sample data used:</p>

<pre><code> cat key
&lt;name&gt;abcdaaefg&lt;/name&gt;
&lt;value&gt;123456&lt;/value&gt;
&lt;name&gt;abcdefg&lt;/name&gt;
&lt;value&gt;123456&lt;/value&gt;
&lt;name&gt;abcdaaefg&lt;/name&gt;
&lt;value&gt;123456&lt;/value&gt;
</code></pre>

<p><code>sed</code> solution:</p>

<pre><code> sed '/abcdefg/!b;n;c&lt;value&gt;OLA&lt;value&gt;' key

&lt;name&gt;abcdaaefg&lt;/name&gt;
&lt;value&gt;123456&lt;/value&gt;
&lt;name&gt;abcdefg&lt;/name&gt;
&lt;value&gt;OLA&lt;value&gt;
&lt;name&gt;abcdaaefg&lt;/name&gt;
&lt;value&gt;123456&lt;/value&gt;
</code></pre>

<p>For doing changes in file. </p>

<pre><code>sed -i.bak '/abcdefg/!b;n;c&lt;value&gt;OLA&lt;value&gt;' key
</code></pre>

<p><code>awk</code> Solution:</p>

<pre><code>awk '/abcdefg/ {print $0;getline;sub(/&gt;.*&lt;/,"&gt;ola&lt;")} {print $0}' key
&lt;name&gt;abcdaaefg&lt;/name&gt;
&lt;value&gt;123456&lt;/value&gt;
&lt;name&gt;abcdefg&lt;/name&gt;
&lt;value&gt;ola&lt;/value&gt;
&lt;name&gt;abcdaaefg&lt;/name&gt;
&lt;value&gt;123456&lt;/value&gt;
</code></pre>

<p>Search for a line containing <code>abcdefg</code> and then do following actions:<br>
1. print that line,<br>
2.move to next line and replace the value inside html tag to something else.  Here , I have replaced <code>123456</code> with <code>ola</code>.   </p>

python xml shell replace sed