title: Bash Using Sed on a Variable
body: <p>I have an xml file which will always have the following block inside of it:</p>
  
  <pre><code>&lt;Name NameType="Also Known As"&gt;
    &lt;NameValue&gt;
      &lt;FirstName&gt;name1&lt;/FirstName&gt;
      &lt;Surname&gt;sur1&lt;/Surname&gt;
    &lt;/NameValue&gt;
    &lt;NameValue&gt;
      &lt;FirstName&gt;name2&lt;/FirstName&gt;
      &lt;Surname&gt;sur2&lt;/Surname&gt;
    &lt;/NameValue&gt;
  &lt;/Name&gt;
  </code></pre>
  
  <p>There may be just one "NameValue" node inside this block or there may be many. My issue is that I only ever want to keep the first one and discard the rest. The example above would hence read:</p>
  
  <pre><code>&lt;Name NameType="Also Known As"&gt;
    &lt;NameValue&gt;
      &lt;FirstName&gt;name1&lt;/FirstName&gt;
      &lt;Surname&gt;sur1&lt;/Surname&gt;
    &lt;/NameValue&gt;
  &lt;/Name&gt;
  </code></pre>
  
  <p>I'd like to use sed for this purpose. I have written the above block to a variable and tried to manipulate that as follows:</p>
  
  <pre><code>var=$(sed -n '/&lt;Name NameType="Also Known As"&gt;*/,/&lt;\/Name&gt;/p' $file)
  sed -i 's/&lt;\/NameValue&gt;.*&lt;\/Name&gt;/&lt;\/NameValue&gt;&lt;\/Name&gt;/g' "$var"
  </code></pre>
  
  <p>I get the following in return:</p>
  
  <pre><code>sed: can't read &lt;Name NameType="Also Known As"&gt;...
  </code></pre>
  
  <p>I thought the above would have replaced everything between the first closing NameValue tag and the closing Name tag with nothing. I used the -i option as I want to save the changes to this file. Perhaps it's syntax related, or maybe my understanding of using the sed command on a variable is way off. A point to note is that these tags occur inside other nodes in the file and so doing one blanket update using sed would change blocks which i don't want to change. This is the reason I passed this block to a variable. Any pointer's in the right direction would be much appreciated. </p>