Convert text file to HTML tabular format.

Please provide script/commands to convert text file to HTML tabular format.

No need of styles and colours, just output and a heading in table is required.

Output file will be send via email and will be seen from outlook.

(script required without using awk).

output file content: (sar command output)

12:00:01 AM       CPU     %user     %nice   %system   %iowait    %steal     %idle
12:10:01 AM       all      5.99      0.00      3.04     11.76      0.00     79.21
12:20:01 AM       all      7.41      0.00      3.05      7.75      0.00     81.79
12:30:01 AM       all     12.79      0.00      2.87      6.74      0.00     77.60
12:40:01 AM       all     10.77      0.00      2.93      8.70      0.00     77.59

Regards,
Veera

Hi,

Simpler than using a script, you could use text2html - it's available here.

Regards

Dave

Hello Veera,

Please use code tags as per forum rules for commands and codes which you are using in post. Following may help you to get a file's output in a html format.
I am taking file named test17 as input file.

Output_File=/tmp/file556
echo "<html>" > $Output_File
echo "<title> Test" >> $Output_File
echo "</title>" >> $Output_File
echo "<body>" >> $Output_File
echo "<table border style="05">" >> $Output_File
 
while read line
do
        echo "<tr>" >> $Output_File
        echo "<td>" >> $Output_File
        echo $line >> $Output_File
        echo "</td>" >> $Output_File
        echo "</tr>" >> $Output_File
done < "test17"
echo "</table>" >> $Output_File
echo "</body>" >> $Output_File
echo "</html>" >> $Output_File
MAILTO=test@test.com
Subject=Test
 
cat - ${Output_File} <<EOF | /usr/sbin/sendmail -oi -t
#From: ${MAILFROM}
To: ${MAILTO}
Subject: $SUBJECT
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
EOF

Thanks,
R. Singh

Thanks R.Singh,

Code works fine, But still you have assigned everything as a row , hence the when I look at the output same issue persists.

sample:

12:00:01 AM       CPU     %user     %nice   %system   %iowait    %steal     %idle
12:10:01 AM       all     5.99     0.00      3.04     11.76      0.00     79.21
12:20:01 AM      all      7.41     0.00      3.05      7.75      0.00     81.79
Average   12.79      0.00      2.87      6.74      0.00     77.60

Hi Veera,

Please let me know the expected output for same. So that I can help more on same.

Thanks,
R. Singh

Hi,

You can always try;

txt2html --make_table --infile "input.txt" --outfile "table.html"

It's quite happy with being included in a script.

Regards

Dave

Expected:

Col1 col2   Col3
1      2      3

current results:

Col1 col2 col3
1 2 3

(this is happens when I send the output to email and viewing it through outlook as its convert to calibri format).

In order to display in tabular format I need to use html and sendmail (for oracle queries I go with SET HTML option). But for Unix related output I need to send as a tabular format.

So instead of reading records into just one variable, read each field into different variables and construct a HTML table.

For the sample input data you posted, you can try something like this:-

#!/bin/ksh

{
        print "From: fromaddr@domain.com"
        print "To: toaddr@domain.com"
        print "MIME-Version: 1.0"
        print "Content-Type: text/html"
        print "Subject: SAR Data"
        print "<html><body>"
        print "<table border=1 cellspacing=0 cellpadding=3>"

        while read tm1 tm2 cpu usr nic sys iow stl idl
        do
                print "<tr>"
                print "<td align=center> $tm1 $tm2 </td>"
                print "<td align=center> $cpu </td>"
                print "<td align=center> $usr </td>"
                print "<td align=center> $nic </td>"
                print "<td align=center> $sys </td>"
                print "<td align=center> $iow </td>"
                print "<td align=center> $stl </td>"
                print "<td align=center> $idl </td>"
                print "</tr>"
        done < inputfile

        print "</table>"
        print "</body></html>"

} | /usr/sbin/sendmail -t