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>Use an XML-aware tool. This will make your approach far more robust: It means that tiny changes in the textual description (like added or removed newlines, or extra attributes added to a preexisting element) won't break your script.</p>

<p>Assuming that your input's structure looks like this (with being under a single parent item, here called <code>item</code>, defining the relationship between a <code>name</code> and a <code>value</code>):</p>

<pre><code>&lt;config&gt;
  &lt;item&gt;&lt;name&gt;abcdef&lt;/name&gt;&lt;value&gt;123456&lt;/value&gt;&lt;/item&gt;
  &lt;item&gt;&lt;name&gt;fedcba&lt;/name&gt;&lt;value&gt;654321&lt;/value&gt;&lt;/item&gt;
&lt;/config&gt;
</code></pre>

<p>...you can edit it like so:</p>

<pre><code> # edit the value under an item having name "abcdef"
 xmlstarlet ed -u '//item[name="abcdef"]/value' -v "new-value"
</code></pre>

<hr>

<p>If instead it's like this (with ordering between name/value pairs describing their relationship):</p>

<pre><code>&lt;config&gt;
  &lt;name&gt;abcdef&lt;/name&gt;&lt;value&gt;123456&lt;/value&gt;
  &lt;name&gt;fedcba&lt;/name&gt;&lt;value&gt;654321&lt;/value&gt;
&lt;/config&gt;
</code></pre>

<p>...then you can edit it like so:</p>

<pre><code># update the value immediately following a name of "abcdef"
xmlstarlet ed -u '//name[. = "abcdef"]/following-sibling::value[1]' -v new-value
</code></pre>

python xml shell replace sed