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

4 Comments
convert text file to html using shell script

There are many cases in which we will need 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.

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.

You might like: 97% OFF on Udemy Shell Scripting Courses

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
    fi

    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>' >> $htm
       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
          done
         echo "</tr>" >> $html
       done < $file
        echo '</table>'
        echo "   </body>" >> $html
        echo "</html>" >> $html
    done

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

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

sudo chmod +x convert.sh

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 convert.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 from this link. Best Linux Udemy Courses

About Dhipu Mathew

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]

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

  • Uk

    I find the below error, please advise.

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

  • Petru

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

  • David Hemsley

    Great script. I enhanced it to
    1) convert any file to file.html.
    2) Read the table header from the first line of the file
    3) Add color to the even rows
    4) Fixed the errors that were mentioned previously

    #!/bin/sh
    #
    # Converts plain text to an HTML table
    #
    if [ $# -eq 0 ]; then
    echo “USAGE: $(basename $0) file1 file2 file3 …”
    exit 1
    fi
    for file in $* ; do
    name=`echo “$file” | awk -F. ‘{print $1}’`
    html=”$name.html”
    echo “” > $html
    echo ”
    table, th, td {
    border: 1px solid black;
    }
    tr:nth-child(even) {background: #CCC}
    tr:nth-child(odd) {background: #FFF}
    ” >> $html
    echo ” ” >> $html
    echo ” >> $html
    #
    # Read table header
    #
    read -ra line < $file
    for i in "${line[@]}"; do
    echo "$i” >> $html
    done
    sed 1d $file | while IFS=’ ‘ read -ra line ; do
    echo “” >> $html
    for i in “${line[@]}”; do
    echo “$i” >> $html
    done
    echo “” >> $html
    done
    echo ” >>$html
    echo ” ” >> $html
    echo “” >> $html
    done

  • ciwol

    Thank you that helped a lot !