Skunk Creek Computing Services

Application Note

Taming HTML Tables with AWK

The TABLE.AWK program

# Table.awk program
# Purpose - Translate table of data values delimited by spaces
#   into html table format
# Input file format -
#   first line - cell titles, makes <th> records
#   successive lines - data values, makes <tr> records
# Field format -
#   field 1 = field1, space terminator
#   field 2 = field2, space terminator ... for NF fields
#   (ensure no spaces in each field; use unique fill character)
# run AWK interpreter:
#   /AWK/AWK table.awk infile > outfile
# Creates entire table, html formatted in the following format:
# <table>                    <!-- linenum -->
# <tr>                       <!-- linenum -->
# <th align=right>Title</th> <!-- linenum -->
#  ...                       <!-- linenum -->
# </tr>                      <!-- linenum -->
#                            <!-- linenum -->
# <tr>                       <!-- linenum -->
#  <td align=right>Data</td> <!-- linenum -->
#  ...                       <!-- linenum -->
# </tr>                      <!-- linenum -->
# ...                        <!-- linenum -->
# </table>                   <!-- linenum -->
# Use redirection to capture output.
# Replace fill character with spaces.
# Delete line 1 - the AWK call.
# The table is now ready to be pasted into the web page skeleton.
#
#               E. Stiltner 97/03/05

NR==1   {                                               # first line
        nfld = NF                                       # obtain n fields
        numout = 0                                      # output line counter
        printf ("%s", "<table>")                        # Begin table
        numout++
        printf ("%s%g%s", "\t<!--", numout, "-->\n")    # line number
        printf ("%s", "<tr>")                           # begin a record
        numout++
        printf ("%s%g%s", "\t<!--", numout, "-->\n")    # line number
        for (i = 1; i <= NF; i++)                       # for n fields
          {
          printf ("%s%s%s", " <th align=right>", $i, "</th>") # title field
          numout++
          printf ("%s%g%s", "\t<!--", numout, "-->\n")  # line number
          }
        printf ("%s", "</tr>")                          # end record
        numout++
        printf ("%s%g%s", "\t<!--", numout, "-->\n\n")  # line number
        numout++
        }

NR>1    {                                               # all following lines
        printf ("%s", "<tr>")                           # begin a record
        numout++
        printf ("%s%g%s", "\t<!--", numout, "-->\n")    # line number
        for (i = 1; i <= NF; i++)                       # for n fields
          {
          printf ("%s%s%s", " <td align=right>", $i, "</td>") # data field
          printf ("%s%g%s", "\t<!--", numout, "-->\n")  # line number
          numout++
          }
        printf ("%s", "</tr>")                          # end record
        numout++
        printf ("%s%g%s", "\t<!--", numout, "-->\n\n")  # line number
        numout++
        }

END     {                                               # after last line
        printf ("%s", "</table>")                       # end of table tag
        numout++
        printf ("%s%g%s", "\t<!--", numout, "-->\n")    # line number
        printf ("%s%g%s", "<!-- ", numout, " HTML records -->\n")
                                                        # total lines
        }                                               # end of program

E. Stiltner


This application note has been brought to you by Skunk Creek Computing Services.

Address comments or otherwise to:
stiltner[AT]sccs.com

Legalities - -
The above information is based on the author's experience and observations. This information is supplied with no warranty nor any guarantee. It is the user's sole responsibility to validate it for his or her intended application.


URL of this page: http://www.sccs.com/sccsaprg.htm
Revised '5-Feb-2003,10:55:18'
Copyright © 1999 SCCS.