I have hundreds of HTML files that are relatively simple in content and maintain a consistent format.
I need to convert them to tables, can I use a shell script to do this?
HTML source code
<html>
<head>
<title>Demo</title>
</head>
<body>
<h1>Page Title</h1>
<div class="row">
<p class="text-1">Text 1</p>
<p class="text-2">Text 2</p>
<p class="text-3">Text 3</p>
<p class="text-4">Text 4</p>
<p class="text-5">Text 5</p>
<p class="text-6">Text 6</p>
</div>
<div class="row">
<p class="text-1">Text 1</p>
<p class="text-2">Text 2</p>
<p class="text-3">Text 3</p>
<p class="text-4">Text 4</p>
<p class="text-5">Text 5</p>
<p class="text-6">Text 6</p>
</div>
<div class="row">
<p class="text-1">Text 1</p>
<p class="text-2">Text 2</p>
<p class="text-3">Text 3</p>
<p class="text-4">Text 4</p>
<p class="text-5">Text 5</p>
<p class="text-6">Text 6</p>
</div>
</body>
</html>
Converted table source code
<table>
<caption>Page Title</caption>
<thead>
<tr>
<th>Hard Code</th>
<th>Hard Code</th>
<th>Hard Code</th>
<th>Hard Code</th>
<th>Hard Code</th>
<th>Hard Code</th>
</tr>
</thead>
<tbody>
<tr>
<td>Text 1</td>
<td>Text 2</td>
<td>Text 3</td>
<td>Text 4</td>
<td>Text 5</td>
<td>Text 6</td>
</tr>
<tr>
<td>Text 1</td>
<td>Text 2</td>
<td>Text 3</td>
<td>Text 4</td>
<td>Text 5</td>
<td>Text 6</td>
</tr>
<tr>
<td>Text 1</td>
<td>Text 2</td>
<td>Text 3</td>
<td>Text 4</td>
<td>Text 5</td>
<td>Text 6</td>
</tr>
</tbody>
</table>
This is the mind map.
Before asking the question, I tried to look up information on the Internet and found that I could extract the HTML content using the pup tool, which is used as follows.
# Extracting page titles
cat demo.html | pup 'body > h1 text{}'
# Extracting paragraph text
cat demo.html | pup 'body > div.row > p.text-1 text{}'
cat demo.html | pup 'body > div.row > p.text-2 text{}'
cat demo.html | pup 'body > div.row > p.text-3 text{}'
cat demo.html | pup 'body > div.row > p.text-4 text{}'
cat demo.html | pup 'body > div.row > p.text-5 text{}'
cat demo.html | pup 'body > div.row > p.text-6 text{}'
In the next I ran into difficulty, I didn't know how to make it into a shell script. It involves shell loops and I spent a few days trying to figure it out, but without success.
Can you all help me? Thank you in advance!
Update
This is what I have tried to do. It has several problems.
- It can only handle one
<div class="row">...</div>data, which is the trickiest problem I've encountered (The problem is shown below). It involves the shell loop problem.
- It can only convert one HTML file at a time, and ideally works in batches of hundreds of HTML files (exported to another directory and saved, file names remain consistent).
#!/usr/bin/env bash
# Extracts HTML content
page_title=$(cat demo.html | pup 'body > h1 text{}')
paragraph_text_a=$(cat demo.html | pup 'body > div.row > p.text-1 text{}')
paragraph_text_b=$(cat demo.html | pup 'body > div.row > p.text-2 text{}')
paragraph_text_c=$(cat demo.html | pup 'body > div.row > p.text-3 text{}')
paragraph_text_d=$(cat demo.html | pup 'body > div.row > p.text-4 text{}')
paragraph_text_e=$(cat demo.html | pup 'body > div.row > p.text-5 text{}')
paragraph_text_f=$(cat demo.html | pup 'body > div.row > p.text-6 text{}')
# Print the contents in a predetermined format
cat << EOF
<table>
<caption>$page_title</caption>
<thead>
<tr>
<th>Hard Code</th>
<th>Hard Code</th>
<th>Hard Code</th>
<th>Hard Code</th>
<th>Hard Code</th>
<th>Hard Code</th>
</tr>
</thead>
<tbody>
<tr>
<td>$paragraph_text_a</td>
<td>$paragraph_text_b</td>
<td>$paragraph_text_c</td>
<td>$paragraph_text_d</td>
<td>$paragraph_text_e</td>
<td>$paragraph_text_f</td>
</tr>
</tbody>
</table>
EOF

sedeven though it is dangerous to do so.