# If you'd like to omit non-matching lines from the result; add ';d' to the end of the expression.
sed -E 's/<h1>([^<]*)<\\/h1>\s*\n([\w\W]*?)(([^\n\S]*)<td\s.*?<\\/td>(\n))(?=\s*<\\/tr>)|(?<=<h1>([^<]*)<\\/h1>[\w\W]*?)(([^\n\S]*)<td\s.*?<\\/td>(\n))(?=\s*<\\/tr>)/$2$3$4$7$8<td class="two">$1$6<\/td>$5$9/g;t' <<< "<h1>TITLE OF HEAD 1</h1>
<table>
<tbody>
<tr>
<td class=\"one\">ITEM 1, AFTER HEAD 1</td>
</tr>
<tr>
<td class=\"one\">ITEM 2, AFTER HEAD 1</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td class=\"one\">ITEM 3, AFTER HEAD 1</td>
</tr>
<tr>
<td class=\"one\">ITEM 4, AFTER HEAD 1</td>
</tr>
<tr>
<td class=\"one\">ITEM 5, AFTER HEAD 1</td>
</tr>
</tbody>
</table>
<h1>TITLE OF HEAD 2</h1>
<table>
<tbody>
<tr>
<td class=\"one\">ITEM 6, AFTER HEAD 2</td>
</tr>
</tbody>
</table>
<h1>TITLE OF HEAD 3</h1>
<table>
<tbody>
<tr>
<td class=\"one\">ITEM 7, AFTER HEAD 3</td>
</tr>
<tr>
<td class=\"one\">ITEM 8, AFTER HEAD 3</td>
</tr>
<tr>
<td class=\"one\">ITEM 9, AFTER HEAD 3</td>
</tr>
<tr>
<td class=\"one\">ITEM 10, AFTER HEAD 3</td>
</tr>
</tbody>
</table>
<h1>TITLE OF HEAD 4</h1>
<table>
<tbody>
<tr>
<td class=\"one\">ITEM 11, AFTER HEAD 4</td>
</tr>
<tr>
<td class=\"one\">ITEM 12, AFTER HEAD 4</td>
</tr>
</tbody>
</table>
"
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for SED, please visit: https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html