cols | If the output from some program runs down the left-hand side of your screen and takes more than one screen to display, you can pipe the program output to a pager (25.3, 25.4). If the lines of text are short, you can see more of that text on the screen at once by reformatting the text into columns. The pr command can make columns (35.17). But it's not easy to use if you want the input text to be ordered down a column instead of across columns. And it's tough to use if you want as many columns as will fit across your screen - you have to find the widest piece of data before you can figure each column's width. |
---|
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:
%spell somefile | cols
word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12 word13 word14 word15 word16
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:
%spell somefile | cols -d
word1 word3 word5 word7 word9 word11 word13 word15 word2 word4 word6 word8 word10 word12 word14 word16
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
(c2, 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
$0
(44.22)
to decide.)
expr (45.28)
does other calculations.
Without the -d flag, the pr command line for making the
columns is simple:
pr -$nc -t -w$width -l1 $temp
The $temp
file holds the input text.
With -d, the command line is more complicated.
It uses
wc -l (29.6)
to count the number of input lines, then expr
to divide by the number of columns and add 1:
pr -$nc -t -w$width -l`expr \( \`wc -l < $temp\` / $nc \) + 1` $temp
The
escaped backquotes (\`
) (45.31)
mean that
wc -l < $temp
will run first.
The line count from wc will be substituted onto the expr command
line.
The result from expr will be glued after the -l
to complete
the pr page length option.
If you don't like condensing the command line that much, you can move the
wc and expr commands to other lines and pass the values with
shell variables.
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:
%tar xvf
archive.tar
cols c2 c3 c4 c5 c6 c7 c8
x cols, 2160 bytes, 5 tape blocks c2 linked to cols c3 linked to cols ...
-