: ########################################################################## # Shellscript: nicecol - print "nice" columnar output # Version : 0.2 (beta) # Author : Heiner Steven (heiner.steven@odn.de) # Date : 10.12.1995 # Category : Text Utilities # SCCS-Id. : $Id: nicecol,v 1.1.1.1 1999/06/15 19:29:05 heiner Exp $ ########################################################################## # Changes # 21.12.95 stv Allow whitespace in output field separator (0.2) # (logan@ptolemy.arc.nasa.gov) ########################################################################## PN=`basename $0` # Program name VER='0.2 (beta)' : ${AWK:=nawk} Usage () { echo >&2 "$PN - produce \"nice\" columnar output, $VER (stv '95) usage: $PN [-i IFS] [-o OFS] [file ...] -i: input field separator (default: whitespace) -o: output field separator (default: blank) Specify a TAB (ASCII 9) output field separator as '\\\t'." exit 1 } #set -- `getopt 'hi:o:' "$@"` || Usage while [ $# -gt 0 ] do case "$1" in -i) ISep="$2"; shift;; # Input field separator -o) OSep="$2"; shift;; # Output field separator --) shift; break;; -h) Usage;; -*) Usage;; *) break;; # first file name esac shift done Data=${TMPDIR:=/tmp}/nc.data.$$ Format=$TMPDIR/nc.fmt.$$ trap 'rm -f "$Data" "$Format" >/dev/null 2>&1' 0 trap "exit 2" 1 2 3 13 15 # First pass: determine field types and sizes cat "$@" | tee "$Data" | $AWK ${ISep:+-F$ISep} ' function Max (a, b) { if ( a > b ) return a; else return b; } { for ( i=1; i<=NF; i++ ) { if ( Type [i] == "string" ) { Width [i] = Max(Width [i], length ($i)) } else { if ( $i ~ /^[0-9][0-9]*$/ && Type [i] != "float" ) { Type [i] = "int" Width [i] = Max(Width [i], length ($i)) Prec [i] = 0 } else if ( $i ~ /^[0-9][0-9]*\.[0-9]*$/ || $i ~ /^[0-9]*\.[0-9][0-9]*$/ ) { Type [i] = "float" k = split ($i, V, ".") Width [i] = Max(length (V [1]), Width [i]) Prec [i] = Max(length (V [2]), Prec [i]) } else { Type [i] = "string" Width [i] = length ($i) } } } if ( NF > MaxFields ) MaxFields = NF } END { for ( i=1; i<=MaxFields; i++ ) { if ( Type [i] == "string" ) { print "%-" Width [i] "s" } else if ( Type [i] == "int" ) { print "%" Width [i] "d" } else if ( Type [i] == "float" ) { print "%" Width[i]+Prec[i]+1 "." Prec[i] "f" } } } ' > $Format || exit # Second pass: format input $AWK ${ISep:+-F$ISep} ' BEGIN { Delim = "'"$OSep"'" if ( Delim == "" ) Delim = " " # Read format specification for each field for ( n=1; getline < "'$Format'"; n++ ) Fmt [n] = $0 close ("'$Format'") } { for ( i=1; i<=NF; i++ ) { printf "" Fmt [i], $i if ( i