#!/usr/bin/awk -f # Convert output from RDBMS products like MySQL or Postgress # to tab separated values with headers. # Eric Blossom December 2003 # Copyright 2003 Blossom Associates West # e.g. # one | two | three # ----+-----+------ # 1 | 2 | 3 # 4 | 5 | 6 # BUG: If one of the column heading is empty, # this will supress all column headings after the first empty one. # However, all columns will still be emitted. # This will not happen with output from RDBM systems. # Trims leading spaces from a string. # Trailing spaces are left intact. function trim( theString ) { n = match( theString, /[^ ]/ ); if ( 0 < n ) { return substr( theString, n ); } return ""; } BEGIN { FS = " *\\| *"; OFS = "\t"; INHEADERS = 1; } /^[-+][-+]*$/ { INHEADERS = 0; for ( i = 1; column[i]; i++ ) { if ( 1 == i ) { printf( "%s", column[i] ); } else { printf( "%s%s", OFS, column[i] ); } } print ""; next; } INHEADERS { for ( i = 1; i <= NF; i++ ) { if ( column[i] ) { column[i] = column[i] " " trim( $i ); } else { column[i] = trim( $i ); } } next; } { $1 = trim( $1 ); print; }