How To Convert Contents of a File to HTML Tables using Shell Script

September 3, 2014 Learning Spot


There are many cases in which we will needing an HTML output for the reports we create using a shell script. For example, if you create a file using a shell script  which contains all the username and their ids from /etc/passwd file, you can convert it into an HTML file for more comprehensible output.

In this tutorial, I will explain how to convert contents of a file to HTML format with proper table headers, rows and columns data.

You might like: Learn Linux in 5 Days and Level Up Your Career


Shell Script to convert contents of a file to HTML Table:

This script converts and creates a new HTML format for the input file. The below script uses “;” delimiter to parse the text file data.  The explanation for running the script is given below the script.

Example input format

Your text file should have values with a delimiter as shown in the example below.

sam; newyork; 1345B; 78sdf3473;
msnam; newsdfyork; 1345B; 783473;
sadfm; newysdfork; 1345B; 7sdf83473;
sam; newysdfork; 1345B; 783sdf473;
sasdfm; newysdfork; 1345B; 7834sdf73;

If the delimiter is other than  “;”, you can change the delimiter in  “while IFS=’;’” to a different delimiter. For example, “while IFS=’-‘”


  #! /bin/bash

    if [ $# -eq 0 ] ; then
       echo "USAGE: $(basename $0) file1 file2 file3 ..."
       exit 1

    for file in $* ; do
       html=$(echo $file | sed 's/\.txt$/\.html/i')

       echo "<html>" >> $html
       echo "<style type="text/css">
            table, th, td {
            border: 1px solid black;
            </style>" >> $html
       echo "   <body>" >> $html
       echo '<table>' >> $html
       echo '<th>HEADING1</th>' >> $html
       echo '<th>HEADING2</th>' >> $html
       echo '<th>HEADING3</th>' >> $html
       echo '<th>HEADING4</th>' >> $html
       while IFS=';' read -ra line ; do
        echo "<tr>" >> $html
        for i in "${line[@]}"; do
           echo "<td>$i</td>" >> $html
         # echo "<td>$i</td>" >> $html
         echo "</tr>" >> $html
       done < $file
        echo '</table>'
        echo "   </body>" >> $html
        echo "</html>" >> $html

1. Save the above script in  .sh format ( for example , )

2. Change the file permissions for execution using the following command.

sudo chmod +x

3. This script takes the text file as an argument . So when you run the script, pass the text file you want to convert as an argument as shown below.

 sudo sh file.txt

4. Once you run the above command , the script will convert all your text data into new HTML file name file.html

5. If you want to convert files other that txt , change the txt format to your desired format in the for loop.

If you want to learn more . You can learn through Linux related courses from Udemy. Click on the below link

Linux related Courses

The following two tabs change content below.
Blogger, designer, developer and thinker. He loves web development and web desinging. He's been working in IT industry for more than 5 years. He loves learning new technologies and design trends . When he is free, he does oil painting. You can reach him at [email protected]

2 Responses to "How To Convert Contents of a File to HTML Tables using Shell Script"

  • Uk May 5, 20157:52 am

    I find the below error, please advise.

    # sh file.txt syntax error at line 9: `html=$’ unexpected

  • Petru December 23, 20151:13 pm

    Hello! Thank you for the script. Can you check it? It have errors. Error message: 23: read: Illegal option -a; 25: Bad substitution

Leave a Reply



Pin It on Pinterest

Hello. Add your message here.
Best Udemy Deal - Get All Courses For $10 REDEEM OFFER