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

Unix Power ToolsUnix Power ToolsSearch this book

45.6. Formatting Plain Text: pr

The line printer spooler (Section 45.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 probably just what you'll 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 66-line page. (You can change that length.) It adds a header that automatically includes the date and time, the filename, 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. Here are a few common options:

Separate pages using formfeed character (^L) instead of a series of blank lines. (This is handy if your pages "creep" down because the printer folds some single lines onto two or three printed lines.)

Replace default header with string str. See Section 21.15.

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 Section 21.15. This is a poor man's paste (Section 21.18).

Separate columns with c (default is a tab).

Omit the page header and trailing blank lines.

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

Begin printing at page num (default is 1).

Produce output having n columns (default is 1). See Section 21.15.

Some versions of pr also support these options:

Multicolumn format; list items in rows going across.

Double-spaced format.

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

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

For output, replace whitespace with field delimiter c (default is a tab) every nth position (default is 8).

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

Offset each line n spaces (default is 0).

Pause before each page. (pr rings the bell by writing an ALERT character to standard error and waits for a carriage-return character to be read from /dev/tty (Section 36.15).)

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.

    sort states_50 | pr -n -5

    If you have an old pr that doesn't support -n, you can use cat -n (Section 12.13) to supply the line numbers:

    sort states_50 | cat -n | pr -5

-- TOR

Library Navigation Links

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