title: Match multiple patterns in xml file, copy and change the matched lines with awk, sed
body: <p>I want to match multiple patterns in xml files, copy and modify the matched lines.
  The xml format is in a multi-line pattern,</p>
  <pre><code>&lt;security-role-assignment&gt;
  ...
  &lt;/security-role-assignment&gt;
  </code></pre>
  <p>which are on separate lines (not on the same line).
  I want to duplicate the block that contains the string &quot;beanA_users&quot; within the multi-line pattern, then replace the duplicated block with:</p>
  <pre><code>s#beanA_users#beanB_users#
  s#runas_a#runas_b#
  </code></pre>
  <p>My environment have awk, sed, grep, but not xml_grep nor xmlstarlet.</p>
  <pre><code>awk -V
  GNU Awk 5.0.0, API: 2.0 (GNU MPFR 4.1.0, GNU MP 6.2.1).
  </code></pre>
  <p>in.xml</p>
  <pre><code>&lt;security-role-assignment&gt;
      &lt;role-name&gt;beanA_users&lt;/role-name&gt;
      &lt;principal-name&gt;runas_a&lt;/principal-name&gt;
  &lt;/security-role-assignment&gt;
  </code></pre>
  <p>out.xml</p>
  <pre><code>&lt;security-role-assignment&gt;
      &lt;role-name&gt;beanA_users&lt;/role-name&gt;
      &lt;principal-name&gt;runas_a&lt;/principal-name&gt;
  &lt;/security-role-assignment&gt;
  &lt;security-role-assignment&gt;
      &lt;role-name&gt;beanB_users&lt;/role-name&gt;
      &lt;principal-name&gt;runas_b&lt;/principal-name&gt;
  &lt;/security-role-assignment&gt;
  </code></pre>
  <p>in2.xml</p>
  <pre><code>&lt;security-role-assignment&gt;
      &lt;role-name&gt;beanXYZ_users&lt;/role-name&gt;
      &lt;principal-name&gt;runas_test&lt;/principal-name&gt;
  &lt;/security-role-assignment&gt;
  &lt;security-role-assignment&gt;
      &lt;role-name&gt;beanA_users&lt;/role-name&gt;
      &lt;principal-name&gt;runas_a&lt;/principal-name&gt;
  &lt;/security-role-assignment&gt;
  &lt;security-role-assignment&gt;
      &lt;role-name&gt;beanC_users&lt;/role-name&gt;
      &lt;principal-name&gt;runas_c&lt;/principal-name&gt;
  &lt;/security-role-assignment&gt;
  </code></pre>
  <p>out2.xml</p>
  <pre><code>&lt;security-role-assignment&gt;
      &lt;role-name&gt;beanXYZ_users&lt;/role-name&gt;
      &lt;principal-name&gt;runas_test&lt;/principal-name&gt;
  &lt;/security-role-assignment&gt;
  &lt;security-role-assignment&gt;
      &lt;role-name&gt;beanA_users&lt;/role-name&gt;
      &lt;principal-name&gt;runas_a&lt;/principal-name&gt;
  &lt;/security-role-assignment&gt;
  &lt;security-role-assignment&gt;
      &lt;role-name&gt;beanB_users&lt;/role-name&gt;
      &lt;principal-name&gt;runas_b&lt;/principal-name&gt;
  &lt;/security-role-assignment&gt;
  &lt;security-role-assignment&gt;
      &lt;role-name&gt;beanC_users&lt;/role-name&gt;
      &lt;principal-name&gt;runas_c&lt;/principal-name&gt;
  &lt;/security-role-assignment&gt;
  </code></pre>
  <p>e.g.</p>
  <pre><code>awk -f test.awk in.xml &gt; out.xml
  awk -f test.awk in2.xml &gt; out2.xml
  </code></pre>
  <p>test.awk (this is my best version so far, still need a lot of work)</p>
  <pre><code>/&lt;security-role-assignment&gt;/ {
     xmlbuf = xmlbuf $0 ORS
     gsub(/beanA_users/,&quot;beanB_users&quot;,xmlbuf)
     gsub(/runas_a/,&quot;runas_b&quot;,xmlbuf)
     print; next
  }
  xmlbuf!=&quot;&quot; {
     printf &quot;%s&quot;, xmlbuf
     xmlbuf=&quot;&quot;
  }
  { print }
  </code></pre>