title: How to extract html between tags
body: <p>I need to extract HTML between two tags using Linux command.</p>
  
  <p>For example: A html document can have multiple <code>&lt;h2&gt;</code> tags like below</p>
  
  <pre><code>&lt;h2&gt;
      &lt;a id="id1" name="name1"&gt;&lt;/a&gt;
      test1
  &lt;/h2&gt;
  &lt;h2&gt;
      &lt;a id="id2" name="name2"&gt;&lt;/a&gt;
      test2
  &lt;/h2&gt;
  </code></pre>
  
  <p>I need to extract inner HTML of all <code>&lt;h2&gt;</code> as array and passed to other php script as a parameter.</p>
  
  <p>Required output:</p>
  
  <pre><code>&lt;a id="id1" name="name1"&gt;&lt;/a&gt;
      test1
  &lt;a id="id2" name="name2"&gt;&lt;/a&gt;
      test2
  </code></pre>
  
  <p>I tried below :</p>
  
  <pre><code>file='/var/www/html/docs/test/test.htm' 
  
  tagHtml=$(grep "&lt;h2&gt;" $file )
  tagHtml=$(echo $metadata_tobe_added | sed 's/&lt;h2&gt;//g' | sed 's/&lt;\/h2&gt;//g')
  echo $tagHtml
  php /var/www/html/test.php "$tagHtml"
  </code></pre>
  
  <p>But nothing working for me. I hope I've been able to make a clear  question. Please help me.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I tried :</p>
  
  <pre><code>awk -F" *&lt;/?h2&gt; *\n?" -v RS="^$" '{
  for(i=2;i&lt;=NF;i+=2)
     printf "%s", $i
  }' $file
  </code></pre>
  
  <p>and i am getting below output which if fine.</p>
  
  <pre><code>&lt;a id="id1" name="name1"&gt;&lt;/a&gt;
  test1
  &lt;a id="id2" name="name2"&gt;&lt;/a&gt;
  test2
  </code></pre>
  
  <p>Now, i want to get the output into array so that i can pass into the php script as a parameter.</p>