#!/usr/bin/awk -f # Copyright 2001 Blossom Associates West # All rights reserved. # Converts files like this to tab separated values. # Must have exactly one line of headers. # Each column heading must be exactly one word. # Second line must be dashes giving the width of the column. # e.g. # ONE TWO THREE # ---------------- ------------ ------------------------------ # 1 2 3 # one one one two threeeeeeeeeee ee BEGIN { OFS = "\t"; } 1 == NR { # The column headings for ( i = 1; i <= NF; i++ ) { head[i] = $i; } next; } 2 == NR { # The underlines #print FIELDWIDTHS; # Seems to be empty. columns = NF; for ( i = 1; i <= NF; i++ ) { width[i] = length( $i ); } next; } { delete cell; offset = 1; for ( i = 1; i <= columns; i++ ) { w = width[i]; cell[i] = substr( $0, offset, w ); sub( "^ *", "", cell[i] ); # trim leading spaces. sub( " *$", "", cell[i] ); # trim trailing spaces. offset += w + 1; } $0 = ""; for ( i in cell ) { $i = cell[i]; } print; }