Convert shell script output txt file to html table

My concnern related to the post
-Convert shell script output txt file to html table,
in this how to print the heading as color.

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

Try

[akshay@localhost tmp]$ cat f
col
1
2
3
4
[akshay@localhost tmp]$ awk 'BEGIN{print "<table>"}{c="td"; e=""}FNR==1{c="th";e=" style=\"color=red;\""}{print "<tr>";for(i=1;i<=NF;i++)printf("<%s%s>%s</%s>\n",c,e,$i,c); print "</tr>"} END{print "</table>"}' f 
<table>
<tr>
<th style="color=red;">col</th>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>3</td>
</tr>
<tr>
<td>4</td>
</tr>
</table>

Readable version

awk '
BEGIN{
            print "<table>"
}
{
    c="td"
    e=""
}
FNR==1{
     c="th";
     e=" style=\"color=red;\""
}
{
         print "<tr>"
         for(i=1;i<=NF;i++)
                   printf("<%s%s>%s</%s>\n",c,e,$i,c);
         print "</tr>"
} 
END{
         print "</table>"
}' infile 
2 Likes

i tried with above code but it is not display the heading as color. no luck.

awk 'BEGIN{print "<table border=1 width=100% cellspacing=0 cellpadding=0 align=center>"}{c="td"; e=""}FNR==1{c="th";e=" style=\"color=red;\""}{print "<tr>";for(i=1;i<=NF;i++)printf("<%s%s>%s</%s>\n",c,e,$i,c); print "</tr>"} END{print "</table>"}' mail_send.txt

can you please suggest me to fix this.

Try <th style="color:red">

Or perhaps <th bgcolor=red style="color:white">

Oh Sorry my mistake,

change

e=" style=\"color=red;\""

to

e=" style=\"color:red;\""

i tried as per your modification even though it does not display the heading as color.

awk 'BEGIN{print "<table border=1 width=100% cellspacing=0 cellpadding=0 align=center>"}{c="td"; e=""}FNR==1{c="th";e=" style=\"color:red;\""}{print "<tr>";for(i=1;i<=NF;i++)printf("<%s%s>%s</%s>\n",c,e,$i,c); print "</tr>"} END{print "</table>"}' mail_send.txt

---------- Post updated at 12:34 AM ---------- Previous update was at 12:32 AM ----------

Hi Chubler,

where to use this code: <th bgcolor=red style="color:white">

i'm confusing ..can you please give me full line code.

Akshay Hegde's (corrected) results display the heading in red. How does your result compare to his? Post your result file. Did you wrap the data in <HTML> tags?

Change the e= assignment to:

e=" bgcolor=red style=\"color:white;\""

so slotting into your 1-liner:

awk 'BEGIN{print "<table border=1 width=100% cellspacing=0 cellpadding=0 align=center>"}{c="td"; e=""}FNR==1{c="th";e=" bgcolor=red style=\"color:white;\""}{print "<tr>";for(i=1;i<=NF;i++)printf("<%s%s>%s</%s>\n",c,e,$i,c); print "</tr>"} END{print "</table>"}' mail_send.txt