I would like to format my table in to html format using awk.
cat table.txt
COL1 COL2 COL4 COL5 COL3
BRCC hete 15869 105A 1
BRAC he 1799967 956G 1
BCAS he 7334543 369AFVC 2
RCA he 9534262 7806-14 4
RCA he 144848 1114A 5
RA he 206118 52A 5
BCAVV he 543304 3807TCD 5
BCA hoo 106091515 4308TDDSC 1
BCA hoo 206075 4563A 1
BCA hoo 799917 2612CDSA 1
BCA hoo 206076 513G 2
BCA hoom 16941 3113A 3
My awk solution:
awk 'BEGIN {print "<table>"} ; { print "<tr><td width="80">" $1 "</td><td width="80">" $2 "</td><td width="150">" $3 "</td><td width="150">" $4 "</td><td>" $5 "</td><tr>"} ; END { print "</table>"}' table.txt
I Have 2 Problems:
1. Problem:
I need to add {print "<table>"}
font style like:
awk 'BEGIN {print "<table style='font-size:8.0pt;font-family:"Verdana","sans-serif"'>"} ;
...
But there is a error with quoting:
awk: cmd. line:1: BEGIN {print "<table style=font-size:8.0pt
awk: cmd. line:1: ^ unterminated string
awk: cmd. line:1: BEGIN {print "<table style=font-size:8.0pt
awk: cmd. line:1: ^ syntax error
zsh: no such file or directory: font-family:Verdana,sans-serif>"} ; { print "<tr><td width="80">" $1 "</td><td width="80">" $2 "</td><td width="150">" $3 "</td><td width="150">" $4 "</td><td>" $5 "</td><tr>"} ; END { print "</table>"}
2. Problem:
And I would like to add "borders" to my table with "======" mark. So output look like:
COL1 COL2 COL4 COL5 COL3
===========================
col1 col2 col4 col5 col3
col1 col2 col4 col5 col3
col1 col2 col4 col5 col3
col1 col2 col4 col5 col3
===========================
A solution using awk
to generate table in html
format
awk 'BEGIN {
split("80,80,150,150,", widths, ",")
print "<style>\
.my_table {font-size:8.0pt; font-family:\"Verdana\",\"sans-serif\"; border-bottom:3px double black; border-collapse: collapse; }\n\
.my_table tr.header{border-bottom:3px double black;}\n\
.my_table th {text-align: left;}\
</style>"
print "<table class=\"my_table\">"
}
NR == 1{
print "<tr class=\"header\">"
tag = "th"
}
NR != 1{
print "<tr>"
tag = "td"
}
{
for(i=1; i<=NF; ++i) print "<" tag " width=\"" widths[i] "\">" $i "</" tag ">"
print "</tr>"
}
END { print "</table>"}' table.txt > table.html
you get in table.html
,
Because I see a sed
tag, you may want to try this:
sed '
1i \
<style> \
.tbl { font-size:8.0pt; font-family:"Verdana", "sans-serif"; border-bottom:3px double black; border-collapse: collapse; } \
.tbl th { border-bottom:3px double black;text-align:left; } \
.tbl td, .tbl th { width:80px; } \
</style> \
<table class="tbl">
1 {
s:[^ ]*:<th>&</th>:g
}
2,$ {
s:[^ ]*:<td>&</td>:g
}
s:^:<tr>:;
s:$:</tr>:
$a </table>
' table.txt
Output::
<style>
.tbl { font-size:8.0pt; font-family:"Verdana", "sans-serif"; border-bottom:3px double black; border-collapse: collapse; text-align:left;}
.tbl th { border-bottom:3px double black; }
.tbl td, .tbl th { width:80px; }
</style>
<table class="tbl">
<tr><th>COL1</th> <th>COL2</th> <th>COL4</th> <th>COL5</th> <th>COL3</th></tr>
<tr><td>BRCC</td> <td>hete</td> <td>15869</td> <td>105A</td> <td>1</td></tr>
<tr><td>BRAC</td> <td>he</td> <td>1799967</td> <td>956G</td> <td>1</td></tr>
<tr><td>BCAS</td> <td>he</td> <td>7334543</td> <td>369AFVC</td> <td>2</td></tr>
<tr><td>RCA</td> <td>he</td> <td>9534262</td> <td>7806-14</td> <td>4</td></tr>
<tr><td>RCA</td> <td>he</td> <td>144848</td> <td>1114A</td> <td>5</td></tr>
<tr><td>RA</td> <td>he</td> <td>206118</td> <td>52A</td> <td>5</td></tr>
<tr><td>BCAVV</td> <td>he</td> <td>543304</td> <td>3807TCD</td> <td>5</td></tr>
<tr><td>BCA</td> <td>hoo</td> <td>106091515</td> <td>4308TDDSC</td> <td>1</td></tr>
<tr><td>BCA</td> <td>hoo</td> <td>206075</td> <td>4563A</td> <td>1</td></tr>
<tr><td>BCA</td> <td>hoo</td> <td>799917</td> <td>2612CDSA</td> <td>1</td></tr>
<tr><td>BCA</td> <td>hoo</td> <td>206076</td> <td>513G</td> <td>2</td></tr>
<tr><td>BCA</td> <td>hoom</td> <td>16941</td> <td>3113A</td> <td>3</td></tr>
</table>