#!/bin/sh # # From N sets of label value pairs introduced by a line with "COLUMN " # or obtained from different files (or both)... # Produce a table with N value columns and a label column, sorted on the labels # # If $SORT is in the environment, it will be used instead of "sort". It # is likely to be something like psort (below) which sorts yymmdd after y2k # # sed 's/^\([0-6][0-9]\)/20\1/; s/^\([789][0-9]\)/19\1/' ${1+"$@"} | # sort | # sed 's/^20//; t # s/^19//; t # ' # If -e is given, table columns 2-N will all be the same width # # Corey Satten, corey@cac.washington.edu, 1/17/97 # http://staff.washington.edu/corey case "$1" in -e) even=1; shift;; esac awk ' { if (NF == 0) next if (FILENAME != ofile || $0 ~ /^COLUMN/) { if (col_width > max_col_width) max_col_width = col_width Col_Width[colct] = col_width; col_width = 0 # store prev value col_name = ofile = FILENAME if ($0 ~ /^COLUMN/) col_name = $2 col_names[++colct] = col_name if (length(col_name) > col_width) col_width = length(col_name) if ($0 ~ /^COLUMN/) next } rows[$1] = 1 if (length($1) > row_width) row_width = length($1) if (length($2) > col_width) col_width = length($2) val[$1 " " col_name] = $2 } END { Col_Width[colct] = col_width row_fmt = sprintf("%%%ds", row_width) for (i=1; i<=colct; ++i) { if (0'"$even"') { # make data columns even width Col_Width[i] = max_col_width } col_fmt[i] = sprintf(" %%%ds", Col_Width[i]) } printf(row_fmt, "!") for (col=1; col<=colct; ++col) printf(col_fmt[col], col_names[col]) printf("\n") for (row in rows) { printf(row_fmt, row) for (col=1; col<=colct; ++col) { v = val[row " " col_names[col]] if ("" v == "") v = "." printf(col_fmt[col], v) } printf("\n") } }' $* | ${SORT-sort}