Unix Power ToolsUnix Power ToolsSearch this book

21.15. Making Text in Columns with pr

The pr command (Section 45.6) is famous for printing a file neatly on a page -- with margins at top and bottom, filename, date, and page numbers. It can also print text in columns: one file per column or many columns for each file.

The -t option takes away the heading and margins at the top and bottom of each page. That's useful when "pasting" data into columns with no interruptions.

21.15.1. One File per Column: -m

The -m option reads all files on the command line simultaneously and prints each in its own column, like this:

% pr -m -t file1 file2 file3

The lines              The lines              The lines
of file1               of file2               of file3
are here               are here               are here
  ...                    ...                    ...

pr may use TAB characters between columns. If that would be bad, you can pipe pr's output through expand. Many versions of pr have a -sX option that sets the column separator to the single character X.

By default, pr -m doesn't put filenames in the heading. If you want that, use the -h option to make your own heading. Or maybe you'd like to make a more descriptive heading. Here's an example using process substitution to compare a directory with its RCS (Section 39.5) subdirectory:

% pr -m -h "working directory compared to RCS directory" <(ls) <(ls RCS)

2000-11-22 23:57  working directory compared to RCS directory  Page    1

0001.sgm                            0001.sgm,v
0002.sgm                            0002.sgm,v
0007.sgm                            0007.sgm,v
0008.sgm                            0008.sgm,v
             ...

(The heading comes from the GNU version of pr. Later examples in this article use a different version with a different heading format.)

21.15.2. One File, Several Columns: -number

An option that's a number will print a file in that number of columns. For instance, the -3 option prints a file in three columns. The file is read, line by line, until the first column is full (by default, that takes 56 lines). Next, the second column is filled. Then, the third column is filled. If there's more of the file, the first column of page 2 is filled -- and the cycle repeats:

% pr -3 file1

Nov  1 19:44 1992  file1  Page 1

Line 1 here            Line 57 here           Line 115 here
Line 2 here            Line 58 here           Line 116 here
Line 3 here            Line 59 here           Line 117 here
  ...                    ...                    ...

The columns aren't balanced -- if the file will fit into one column, the other columns aren't used. You can change that by adjusting -l, the page length option; see the section below.

21.15.3. Order Lines Across Columns: -l

Do you want to arrange your data across the columns, so that the first three lines print across the top of each column, the next three lines are the second in each column, and so on, like this?

% pr -l1 -t -3 file1
Line 1 here            Line 2 here            Line 3 here
Line 4 here            Line 5 here            Line 6 here
Line 7 here            Line 8 here            Line 9 here
  ...                    ...                    ...

Use the -l1 (page length 1 line) and -t (no title) options. Each "page" will be filled by three lines (or however many columns you set). You have to use -t; otherwise, pr will silently ignore any page lengths that don't leave room for the header and footer. That's just what you want if you want data in columns with no headings.

If you want headings too, pipe the output of pr through another pr:

% pr -l1 -t -3 file1 | pr -h file1

Nov  1 19:48 1992  file1  Page 1

Line 1 here            Line 2 here            Line 3 here
Line 4 here            Line 5 here            Line 6 here
Line 7 here            Line 8 here            Line 9 here
  ...                    ...                    ...

The -h file1 puts the filename into the heading.

Also see paste (Section 21.18). Of course, programming languages like awk (Section 20.10) and perl (Section 41.1) can also make text into columns.

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.