我有一个使用 bash 脚本处理的 html 文件,并且想要删除空表。该文件是从 sql 语句生成的,但在未找到记录时包含表头。我想删除没有找到记录的标题。
<table border="1">
<caption>Table with data</caption>
<tr>
<th align="center">type</th>
<th align="center">column1</th>
<th align="center">column2</th>
<th align="center">column3</th>
<th align="center">column4</th>
</tr>
Data rows exists here
</table>
<table border="1">
<caption>Empty Table To Remove</caption>
<tr>
<th align="center">type</th>
<th align="center">column1</th>
<th align="center">column2</th>
<th align="center">column3</th>
<th align="center">column4</th>
<th align="center">column5</th>
<th align="center">column6</th>
<th align="center">column7</th>
</tr>
</table>
<table border="1">
<caption>Table with data</caption>
<tr>
<th align="center">type</th>
<th align="center">column1</th>
<th align="center">column2</th>
<th align="center">column3</th>
<th align="center">column4</th>
</tr>
Data rows exists here
</table>
我尝试使用 grep 和 sed 的组合来删除空表。当表包含相同数量的列时,我能够完成此任务。我现在遇到了一些问题,因为我的表的列数不同。
当表具有相同数量的列时,我能够根据标题进行循环,进行计数,然后删除。由于列数不同,这不起作用。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
像这样,使用 xmlstarlet 和 xpath:
要在
sed -i等位置进行编辑,请使用没有解释,但是不要使用
sed或regex来解析HTML/XML