home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 43.6 Printing to a Terminal Printer Chapter 43
Next: 43.8 Fixing Margins with pr and fold

43.7 Quick-and-Dirty Formatting Before Printing

The line printer spooler ( 43.2 ) prints what you send it. If you send it a continuous stream of text (and the printer is set up to print text files rather than PostScript), that's just what you'll probably get: no page breaks, indenting, or other formatting features.

That's where pr comes in. It's a simple formatter that breaks its input into "pages" that will fit onto a standard 66-line page. (Well, US standard anyway.) It adds a header that automatically includes the date and time, the file name, and a page number. It also adds a footer that ensures that text doesn't run off the bottom of the page.

This is just what you want if you are sending program source code or other streams of unbroken text to a printer. For that matter, pr is often very handy for sending text to your screen. In addition to its default behavior, it has quite a few useful options:


Separate pages using formfeed character (^L) instead of a series of blank lines.

-h str

Replace default header with string str . See article 35.17 .

-l n

Set page length to n (default is 66).


Merge files, printing one in each column (can't be used with -num and -a ). Text is chopped to fit. See article 35.17 . This is a poor man's paste ( 35.18 ) .

-s c

Separate columns with c (default is a tab).


Omit the page header and trailing blank lines.

-w num

Set line width for output made into columns to num (default is 72).

+ num

Begin printing at page num (default is 1).

- n

Produce output having n columns (default is 1). See article 35.17 .

There are also options that apply only to the System V version:


Multi-column format; list items in rows going across.


Double-spaced format.

-e cn

Set input tabs to every n th position (default is 8), and use c as field delimiter (default is a tab).


Fold input lines (avoids truncation by -a or -m ).

-i cn

For output, replace white space with field delimiter c (default is a tab) every n th position (default is 8).

-n cn

Number lines with numbers n digits in length (default is 5), followed by field separator c (default is a tab). See also nl .

-o n

Offset each line n spaces (default is 0).


Pause before each page.


Suppress messages for files that can't be found.

Let's put this all together with a couple of examples:

  • Print a side-by-side list, omitting heading and extra lines:

    pr -m -t list.1 list.2 list.3

  • Alphabetize a list of states; number the lines in five columns. First, with the System V options:

    sort states_50 | pr -n -5

    On a BSD system, which doesn't support -n , you can use cat -n ( 25.21 ) to supply the line numbers:

    sort states_50 | cat -n | pr -5

    To get output on BSD that's identical to System V, you'll need to set a column length:

    sort states_50 | cat -n | pr -t -5 - | 10 | pr -h states_50


Previous: 43.6 Printing to a Terminal Printer UNIX Power Tools Next: 43.8 Fixing Margins with pr and fold
43.6 Printing to a Terminal Printer Book Index 43.8 Fixing Margins with pr and fold

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System