Generating an HTML Table Using Shell Script

If you ever need to generate an HTML table from file data, here is a basic shell script that should do the trick. If you’d rather use Python, check out the Generating an HTML Table Using Python 3 tutorial.

#!/bin/bash

# Run as: table.sh < {input-file-name} > {output-file-name}
# The script requires a space-delimited data file to parse into an html table.
# It does not automatically create a header row.

echo \<table\>
while read line; do
    echo \<tr\>
    for item in $line; do
        echo \<td\>$item\<\/td\>
    done
    echo \<\/tr\>
done
echo \<\/table\>

Input File

The input file must contain data in a space-delimited format. For example,

No. Package Priority Dependency?
1 adduser important yes
2 apt important yes
3 apt-utils important no
4 base-files required yes
5 base-passwd required yes
6 bash required no
7 bsdmainutils important no
8 bsdutils required yes
9 coreutils required yes
10 cpio important yes
11 cron important yes
12 dash required yes
13 debconf required yes
14 debconf-i18n important no
15 debian-archive-keyring important yes
16 debianutils required yes
17 diffutils required no
18 dmidecode important no
19 dmsetup optional yes
20 dpkg required yes

The shell script uses redirected input from the data file to generate the HTML table. File data is redirected into the table.sh script via the “< {input-file-name}” command.

Given that the script requires space-delimited data, it will not properly handle data fields that contain spaces (e.g., “Is a Dependency?”). Script modification is needed to handle these cases.

Output File

The output file will be created via standard output redirection using “> {output-file-name}” to create and write to an output file. The file will contain the input file’s data formatted into a HTML table structure. For example,

<table>
<tr>
<td>No.</td>
<td>Package</td>
<td>Priority</td>
<td>Dependency?</td>
</tr>
<tr>
<td>1</td>
<td>adduser</td>
<td>important</td>
<td>yes</td>
</tr>
<tr>
<td>2</td>
<td>apt</td>
<td>important</td>
<td>yes</td>
</tr>
<tr>
<td>3</td>
<td>apt-utils</td>
<td>important</td>
<td>no</td>
</tr>
<tr>
<td>4</td>
<td>base-files</td>
<td>required</td>
<td>yes</td>
</tr>
<tr>
<td>5</td>
<td>base-passwd</td>
<td>required</td>
<td>yes</td>
</tr>
<tr>
<td>6</td>
<td>bash</td>
<td>required</td>
<td>no</td>
</tr>
<tr>
<td>7</td>
<td>bsdmainutils</td>
<td>important</td>
<td>no</td>
</tr>
<tr>
<td>8</td>
<td>bsdutils</td>
<td>required</td>
<td>yes</td>
</tr>
<tr>
<td>9</td>
<td>coreutils</td>
<td>required</td>
<td>yes</td>
</tr>
<tr>
<td>10</td>
<td>cpio</td>
<td>important</td>
<td>yes</td>
</tr>
<tr>
<td>11</td>
<td>cron</td>
<td>important</td>
<td>yes</td>
</tr>
<tr>
<td>12</td>
<td>dash</td>
<td>required</td>
<td>yes</td>
</tr>
<tr>
<td>13</td>
<td>debconf</td>
<td>required</td>
<td>yes</td>
</tr>
<tr>
<td>14</td>
<td>debconf-i18n</td>
<td>important</td>
<td>no</td>
</tr>
<tr>
<td>15</td>
<td>debian-archive-keyring</td>
<td>important</td>
<td>yes</td>
</tr>
<tr>
<td>16</td>
<td>debianutils</td>
<td>required</td>
<td>yes</td>
</tr>
<tr>
<td>17</td>
<td>diffutils</td>
<td>required</td>
<td>no</td>
</tr>
<tr>
<td>18</td>
<td>dmidecode</td>
<td>important</td>
<td>no</td>
</tr>
<tr>
<td>19</td>
<td>dmsetup</td>
<td>optional</td>
<td>yes</td>
</tr>
<tr>
<td>20</td>
<td>dpkg</td>
<td>required</td>
<td>yes</td>
</tr>
</table>

Output File Rendered in Website’s CSS

Here is the output file’s HTML table rendered with the CSS formatting used by this website.

No. Package Priority Dependency?
1 adduser important yes
2 apt important yes
3 apt-utils important no
4 base-files required yes
5 base-passwd required yes
6 bash required no
7 bsdmainutils important no
8 bsdutils required yes
9 coreutils required yes
10 cpio important yes
11 cron important yes
12 dash required yes
13 debconf required yes
14 debconf-i18n important no
15 debian-archive-keyring important yes
16 debianutils required yes
17 diffutils required no
18 dmidecode important no
19 dmsetup optional yes
20 dpkg required yes