title: Replace section of an xml file with awk or sed
body: <p>I'd like to replace the section below between <code>&lt;restApi&gt;</code> and <code>&lt;/restApi&gt;</code> (including restApi tags) with another string:</p>
  
  <pre><code>...
    &lt;restApi&gt;
      &lt;baseUrl&gt;https://domain.com/nexus&lt;/baseUrl&gt;
      &lt;forceBaseUrl&gt;true&lt;/forceBaseUrl&gt;
      &lt;uiTimeout&gt;60000&lt;/uiTimeout&gt;
    &lt;/restApi&gt;
  ...
  </code></pre>
  
  <p>Using awk, I use the following command to do the replacement :</p>
  
  <pre><code>awk '/&lt;restApi&gt;/,/&lt;\/restApi&gt;/ {sub(/.*/,"&lt;sometag&gt;stuff&lt;/sometag&gt;")}1' file.xml
  </code></pre>
  
  <p>The problem is that awk replaces each line with the replacement string so I'm getting this output:</p>
  
  <pre><code>...
  &lt;sometag&gt;stuff&lt;/sometag&gt;
  &lt;sometag&gt;stuff&lt;/sometag&gt;
  &lt;sometag&gt;stuff&lt;/sometag&gt;
  &lt;sometag&gt;stuff&lt;/sometag&gt;
  &lt;sometag&gt;stuff&lt;/sometag&gt;
  ...
  </code></pre>
  
  <ul>
  <li>What am I missing in the awk command to have only one <code>"&lt;sometag&gt;stuff&lt;/sometag&gt;"</code> in the result?</li>
  <li>How do I do it with sed instead?</li>
  <li>Spacing/tabs get lost during replacement. How can I preserve it?</li>
  </ul>