35.16 Make Columns Automatically with cols
Some UNIX systems have a program specifically for making data into columns - but many don't. The cols script takes care of that. It reads your text, finds the widest piece, and chooses pr options to make as many columns as will fit on the screen. cols also has seven other names- links ( 18.3 ) named c2 , c3 , c4 , c5 , c6 , c7 , and c8 -that make output in 2, 3, 4, 5, 6, 7, or 8 columns. If you call the script with one of those names, it will fill the screen with that number of columns. For example, to list misspelled words in columns:
% By default, like the example above, cols and the others order the input words across the screen; that's the fastest way. If you need output going down each column, use the -d option. Then the scripts will calculate column length and order items down the screen:
% The script will read from files you name; otherwise it reads standard input. It figures your screen width from the COLUMNS environment variable if it's set; otherwise, it calls tcap ( 41.10 ) to read your termcap . (On terminfo systems, use tput ( 41.10 ) instead of tcap .) If you use a windowing system with adjustable-width windows, the script could be hacked to check the output of stty size or stty -g ( 42.4 ) .
A few programming details:
the number of columns,
nc
, is taken from the script name
(
c
2
, etc.)-or, if you call
cols
,
the script uses
awk
(
33.11
)
to find the longest input line and calculate the number of columns.
(A
case
statement (
44.5
)
tests
pr -$nc -t -w$width -l1 $temp
The
pr -$nc -t -w$width -l`expr \( \`wc -l < $temp\` / $nc \) + 1` $temp
The
escaped backquotes ( You can install this script from the CD-ROM or from the online archive ( 52.7 ) . If you get it from the archive, ask tar to install cols and its seven other links:
% - |
|