Groby Computers Ltd - Leicester
Oracle Database Administration
Unix Script: table                   # simple data tabulator
This script takes a file or standard input and tabulates the data without the need for it to contain directives
as is the case with for ( for example ) nroff or troff.
Example:
# ls /var | table -b -c4
-----------------------------------------------------------------------------------------
| account | cache | db | empty |
| games | gdm | lib | local |
| lock | log | mail | nis |
| opt | preserve | run | spool |
| tmp | www | yp | |
-----------------------------------------------------------------------------------------
The script is in two parts. First is a Korn-Shell script "table" that is the executable part and second is an associated awk
script. Both parts are required.
Instructions:
- Cut and past the table script and save into a directory in your path.
- Make table executable using
chmod +x table
- Cut-and-paste the table.awk script, putting it in the same directory as the ksh part
table
#!/bin/ksh
#
# Print a table with c columns each w characters wide with a left-margin of m spaces.
#
bin=`dirname $0`
b=0
F="$( echo x | tr x '\011' )"
while getopts bc:m:w:I:H:V:i: name
do
case $name in
b) b=$(( (b-1) * -1 ))
I="|";; # toggle outer box off (0) or on ( not zero )
c) c=$OPTARG;; # number of columns
m) m=$OPTARG;; # left margin width ( characters )
w) w=$OPTARG;; # column width ( characters ) excluding internal separator
I) I="$OPTARG";; # Internal column separator character
H) H="$OPTARG";; # Box Horizontal line charater
V) V="$OPTARG";; # Box Vertical line character
F) F="$OPTARG";; # Field Separator
i) i=$OPTARG;; # Left column indent
esac
done
shift $(($OPTIND -1))
cat $1 |
awk -vFS="$F" '{ for (i=1;i<=NF;i++) print $i }' |
awk -f $bin/table.awk -vncols=$c -vmargin=$m -vwidth=$w -vbox=$b -vIsep="$I" -vHbox=$H -vVbox=$V -vindent=$i -vFS="$F"
# eof table
table.awk
BEGIN \
{
if ( width == "" ) width = 20 ; if ( width < 1 ) width = 1
if ( ncols == "" ) ncols = 6 ; if ( ncols < 1 ) ncols = 1
if ( margin == "" ) margin = 6 ; if ( margin < 0 ) margin = 0
if ( indent == "" ) indent = 1 ; if ( indent < 0 ) indent = 0
if ( Isep == "" ) Isep = " "
if ( Vbox == "" ) Vbox = "|"
if ( Hbox == "" ) Hbox = "-"
if ( box == "" ) box = 0
Lindent = ""
for ( i=0;i<indent;i++) Lindent = Lindent " "
Lmargin = ""
for ( i=0;i<margin;i++) Lmargin = Lmargin " "
for ( i=1;i<=ncols;i++ ) cell[i] = sprintf("%s%"width"s",Lindent," ")
if ( box == 0 )
{
Vbox = ""
}
else
{
rlen = length(Vbox) + ( ncols * (width+indent) ) + ( (ncols-1) * length(Isep) ) + length(Vbox)
Hline = ""
for ( i=0;i<rlen;i++ ) Hline = Hline Hbox
}
row = 0
col = 0
}
{
if ( row == 0 && box != 0 ) printf "%s%s\n", Lmargin, Hline
row++
for (i=1;i<=NF;i++)
{
col++
cell[col] = sprintf("%s%-"width"s",Lindent,$i)
if ( col == ncols )
{
for (col=1;col<=ncols;col++)
{
if ( col == 1 )
{ sep = Lmargin Vbox }
else
{ sep = Isep }
printf "%s%s", sep, cell[col]
cell[col] = sprintf("%s%"width"s",Lindent," ")
}
printf "%s\n", Vbox
col = 0
}
}
}
END \
{
if ( col > 0 )
{
for (col=1;col<=ncols;col++)
{
if ( col == 1 )
{ sep = Lmargin Vbox }
else
{ sep = Isep }
printf "%s%s", sep, cell[col]
}
printf "%s\n", Vbox
}
if ( box != 0 ) printf "%s%s\n", Lmargin, Hline
}
That is all
Page Updated Thu Oct 13 21:43:39 BST 2011