#!/usr/bin/awk -f # Copyright 2000 Blossom Associates West # All rights reserved. # Transforms a table of tab separated values into an HTML table. # # You can specify a particular row to be column headings with hrow. # The first row is 1. The last row is -1. # 0, the default indicates that there are no column headings. Just data. # # Similarily, you can specify a particular column as containing row labels with hcol. # # If you specify a row of column headings, # this script will look at each heading for hints on column alignment. # If the heading starts with a space, then the column will be right justified. # If the heading ends with a space, then the column will be centered. # # You can define colFmt to be a string of column alignment specifiers. # Each position in the string corresponds to a column. # Values are < meaning left, > meaning right, and | meaning centered. # This specification will override any hints in the hrow headings mentioned above. # # e.g. tsv2html.awk hrow=1 hcol=-1 colFmt="<<><|" input BEGIN { FS = "\t"; hrow = 0; # Row with column headings. hcol = 0; # Column with row labels. tablePrefix = ""; tableSuffix = "
"; numCols = 0; rowPrefix = ""; rowSuffix = ""; cellFormat = "%s"; headCellFormat = "%s"; } 1 == NR { for ( i = 1; i <= length( colFmt ); i++ ) { c = substr( colFmt, i, 1 ); if ( ">" == c ) { colAlign[i] = "right"; } if ( "<" == c ) { colAlign[i] = "left"; } if ( "|" == c ) { colAlign[i] = "center"; } } if ( tablePrefix ) { print tablePrefix; } } hrow == NR { for ( i = 1; i <= NF; i++ ) { colAlign[i] = "left"; if ( " " == substr( $i, 1, 1 ) ) { colAlign[i] = "right"; $i = substr( $i, 2 ); # trim the indicator } if ( " " == substr( $i, length( $i ) ) ) { colAlign[i] = "center"; $i = substr( $i, 1, length( $i ) - 1 ); # trim the indicator } if ( ">" == substr( colFmt, i, 1 ) ) { colAlign[i] = "right"; } if ( "<" == substr( colFmt, i, 1 ) ) { colAlign[i] = "left"; } if ( "|" == substr( colFmt, i, 1 ) ) { colAlign[i] = "center"; } } } { for ( i = 1; i <= NF; i++ ) { cell[NR,i] = $i; } if ( numCols < NF ) { numCols = NF; } } END { if ( 0 + hrow < 0 ) hrow = NR + 1 + hrow; if ( 0 + hcol < 0 ) colw = numCols + 1 + colw; for ( i = 1; i <= NR; i++ ) { printf( rowPrefix ); for ( j = 1; j <= numCols; j++ ) { if ( i == hrow || j == hcol ) { printf( headCellFormat, colAlign[j], cell[i,j] ); } else { printf( cellFormat, colAlign[j], cell[i,j] ); } } print rowSuffix; } if ( tableSuffix ) { print tableSuffix; } }