Linux Bash: Easily delete HTML table data blocks
P粉787806024
P粉787806024 2024-04-02 09:13:43
0
1
433

I have an html file that is processed using a bash script and want to delete empty tables. The file is generated from the sql statement but includes headers when no record is found. I want to delete the title for which no record was found.

<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>

I tried using a combination of grep and sed to delete the empty table. I am able to accomplish this task when the tables contain the same number of columns. I'm having some problems now because my tables have different number of columns.

When the tables have the same number of columns, I am able to loop based on the headers, count, and then delete. Since the number of columns is different, this doesn't work.

P粉787806024
P粉787806024

reply all(1)
P粉242741921

Like this, use and :

$ xmlstarlet format -H file.html | sponge file.html
$ xmlstarlet ed -d '//table[./caption/text()="Empty Table To Remove"]' file.html 



  
    
Data rows exists here
Table with data
type column1 column2 column3 column4
Data rows exists here
Table with data
type column1 column2 column3 column4

To edit in a location such as sed -i, use

xmlstarlet edit -L ...

No explanation, but do not use sed or regex to parse HTML/XML

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!