Convert shell script output txt file to html table

Hi,

I have script which generates the output as below:

Jobname Date           Time        Status
abc        12/9/11       17:00      Completed
xyz         13/9/11       21:00      Running

I have the output as a text file. I need to convert it into a HTML Table and sent it thru email

Thanks

kent$  echo "Jobname Date Time Status
abc 12/9/11 17:00 Completed
xyz 13/9/11 21:00 Running"|awk 'BEGIN{print "<table>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print "</table>"}'
<table>
<tr>
<td>Jobname</td>
<td>Date</td>
<td>Time</td>
<td>Status</td>
</tr>
<tr>
<td>abc</td>
<td>12/9/11</td>
<td>17:00</td>
<td>Completed</td>
</tr>
<tr>
<td>xyz</td>
<td>13/9/11</td>
<td>21:00</td>
<td>Running</td>
</tr>
</table>

Hi sk1418,

Thanks for your suggestion.

Is there a way to get the input directly from the txt file instead of coding it.

yes.. use..

so simple :slight_smile:

Hi,

With your suggestions i created the below script but the output email just has tags not the table view. Please help..

#!/bin/bash

echo 'Content-Type: text/html; charset="us-ascii" ' >> email.html

echo "<html>" >> email.html

echo "<Body>" >> email.html

awk 'BEGIN{print "<table>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print "</table>"}' test >> email.html

echo "</Body>" >> email.html

echo "</html>" >> email.html

mail -s "test" abc@xyz.com < email.html

You should use the actual input file as you have shown in #1.
the content of the file should be like this.

Jobname Date Time Status
abc 12/9/11 17:00 Completed
xyz 13/9/11 21:00 Running

you need to use sendmail utility to send email in html format.

Try the below code, instead of mail command

 
(
echo "From: abcd@abcd.com "
echo "To: abcd@abcd.com "
echo "MIME-Version: 1.0"
echo "Subject: Test HTML e-mail." 
echo "Content-Type: text/html" 
cat email.html
) | sendmail -t