#!/usr/bin/awk -f
# Copyright 2000 Blossom Associates West
# All rights reserved.
# Makes an HTML table out of a table of tab separated values.
# Some extra formatting can be supplied by setting tablePrefix.
# e.g. tsv2html.awk tablePrefix="
| column 1 | column 2 |
" input
# You can specify a row containing column headings with hrow.
# If hrow is 0 (the default), then all rows are treated as data.
# e.g. tsv2html.awk hrow=1 input
# You can also override the OFS and/or ROWFORMAT
# e.g. tsv2html.awk hrow=1 ROWFORMAT="| %s |
" input
BEGIN {
hrow = 0;
FS = "\t";
OFS = "";
tablePrefix = "";
ROWFORMAT = " | | %s |
\n"
}
1 == FNR {
if ( FNR != NR ) {
if ( tableSuffix ) {
print tableSuffix;
}
}
if ( tablePrefix ) {
print tablePrefix;
}
}
hrow == FNR {
originalOFS = OFS;
gsub( /td/, "th", OFS );
originalROWFORMAT = ROWFORMAT;
gsub( /td/, "th", ROWFORMAT );
}
fstart && $0 ~ fstart{
print "";
fstart = "This could never appear in the input. Could it? !@#$%^&*()";
}
{
if ( 0 < NF ) { # Replace field separators.
$1 = $1;
}
printf( ROWFORMAT, $0 );
}
hrow == FNR {
OFS = originalOFS;
ROWFORMAT = originalROWFORMAT;
}
END {
if (fstart) {
print ""
}
if ( tableSuffix ) {
print tableSuffix;
}
}