Previous Table of Contents Next


Module 105
pr

DESCRIPTION

The external pr command formats files for printing or displaying. It reads from the standard input or from a list of files and writes to the standard output. The input is reformatted according to specified options. The formatting is page and file oriented. Textual type formatting, as done by troff, is not performed by pg. The output can be reformatted to multiple columns, with or without headers and footers, indented, and double spaced, among various other formats.

COMMAND FORMAT

Following is the general format of the pr command.

     pr [ [ -col ] [ -wwid ] [ -a ] ] [ +p ] [ -dfprt ] [ -eck ]
      [ -hhdr ] \ [ -ick ] [ -llen ] [ -nck ] [ -ooffset ]
      [ -sc ] [ -F ] [ filelist ] \ [ - ]

     pr [ [ -m ]   [ -wwid ]        ] [ +p ] [ -dfprt ] [ -eck ]
      [ -hhdr ] \ [ -ick ] [ -llen ] [ -nck ] [ -ooffset ]
      [ -sc ] [ -F ] [ file_list ] \ [ - ]

The first format is used to specify the number of columns (-col) to display on the output. The second format displays the output based on the number of input files. If you have three input files, the output is displayed in three columns. The -col option and the -m option are mutually exclusive; that is, you can only use one or the other on the same command line.


BSD (Berkeley)
pr [ -col ] [ +p ] [ -f ] [ -hhdr ] [ -llen ] [ -m ] [ -sc ]
[ -t ] \ [ -wwid ] [ file_list ]
The -col option does not appear to work on some BSD type systems.

Options

The following list describes the options and their arguments that may be used to control how pr functions.

+p Begins displaying text at page p. The p must be an integer. The page numbers are based on the -l and -h options. The default number for p is 1. For example,

pr +5 infile

skips the first 226 (4 pages * 56 lines per page) lines of input and displays the fifth page of input.

-col Displays the output in col columns. The -e and -i options are assumed. This option cannot be used in conjunction with the -m option. The input is displayed down the first column of output then down the second column. This process is repeated for each page of output. The column widths vary depending on the page width and number of columns requested. By default, input lines that are too long to be displayed in the column are truncated. For example,
pr -4 infile


data data data data
. .

displays the data in 4 columns; each page has a header.

-a Displays multicolumn output (-col option) across the columns instead of down the columns. The first line of input goes in column one, the second line goes in column two, and so on. This option can only be used with the -col option.
-d Double space the output. Blank lines at the top of the page are dropped.
-eck Expands input character c to k positions in the output. The default for c is a tab. The c must be a nondigit. The default number for k is 8. Therefore, the tab positions are set to 1, 9, 17, and so on. You can omit either the c or the k or both. If k is given, the output tab positions are set to k + 1, 2 * k + 1, and so on. For example, if you specify

pr -e:20 infile

each : (colon) found in the input line is expanded to the next tab position that is a multiple of 20. The character following the first colon is placed in column 21 if possible.

-f Use form feed character (Ctrl-L) for new pages. Normally a sequence of new-lines are used to eject a page. A pause occurs before the first page is printed. You MUST press Return to have pr continue.
-F Fold the input lines to fit the current line width. If used with the -a or -m options for multicolumn output lines will be folded to fit the current columns width.
-h hdr Use hdr as the text for the header instead of the filename. The -h option is ignored if the -t option is specified or the -l option is specified with a length of 10 or less. The -h must be followed by a space. The hdr may be enclosed in quotes if it contains blanks. For example,

pr -h "infile - Please deliver to mylogin" infile

places the message "infile-Please deliver to mylogin" on the top of each page.

-ick Tabs replace spaces in the output where possible. The default for c is a tab. The c must be a nondigit. The default for k is 8; therefore, tab positions are set at 1, 9, 17, and so on. If k is given, then tab positions are set at k + 1, 2 * k + 1, and so on.
-llen Set the page length to len lines. The default is 66 lines. A len of zero (0) is reset to 66. If len is less than 10 then the -t option is assumed and the header and trailers are suppressed. If the headers and trailers are not suppressed, the actual lines of text displayed per page is len - 10. The header and trailer require 10 lines per page. Thus a normal page only displays 56 lines of text.
-m Merges and displays all input files simultaneously in multiple columns. Each input file is displayed in its own column. The most files you can specify is eight. If lines are longer than the width of the column, they are truncated. The -m option cannot be used with the -col or -a options.
-nck Generates numbered output lines. The line numbers are formatted in k spaces. The default for k is five. If c is specified, then the line number is separated from the text by character c.
-ooffset Offset (indent) each line by offset characters. The default of offset is zero (0).
-p Pauses before displaying the next page of output. The output must be to your terminal screen. Your terminal's bell is sounded and pr waits for you to press Return before displaying the next page.
-r No diagnostic warning messages are displayed if a problem occurs while pr is attempting to open a file.
-sc Separate columns of output with the character c instead of the appropriate number of spaces used for normal formatting. The default character for c is the tab. Multicolumn output is not truncated unless the -w option is specified.
-t Suppress the headers and trailers. The normal header has two blank lines: an information line followed by two more blank lines. The information line contains the Month, Day, Time, Year, Filename, and Page number. The trailer consists of five blank lines. The -t option causes the -h option to be ignored.
-wwid Sets the width of the output line to wid characters. The normal width is 72 characters. This option only affects multicolumn output (-col and -m options).
If no options are specified, a header and footer are added to each page of output. The header consist of five lines, with the third line containing the Month, Day, Time, Year, Filename, and Page number. The footer is five blank lines. For example,

pr infile

produces

Mar 16 06:57 1989 infile Page 1
First line of input.
@LINE=

BSD (Berkeley)
The supported options under BSD function the same as the corresponding SV options.

Arguments

The following list describes the arguments that may be passed to the pr command.

filelist One or more files to be formatted by pr
file1 The first file used for the -m option. The -m option requires two input streams (files).
file2 The second file used for the -m option

FURTHER DISCUSSION

You can use pr to format files to your terminal screen or to a printer using the lp (lpr-BSD) command. If you have a printer connected to the auxiliary port of your terminal, the -f option comes in handy. You can enter your pr -f infile command and wait for pr to beep your terminal. Then you can align your printer paper and activate the printer port on the terminal. Press Return to have pr continue displaying your file.

Reformatting the data is useful. You may have a file that has tabs in it. Because the tabs are set at every 8 spaces, the text extends off the end of your printer paper. To resolve the problem you can use pr. For example,

     pr -e4 infile

reduces each tab to four spaces instead of eight. This way you can squeeze more tab filled text on a narrower piece of paper or screen.

You will find it helpful to use pr to send files to the system printer. By using pr you can generate formatted listings of your program source code or various other text files. For example,

     pr -n -e4 mysrc.c | lp -dlaser

formats the mysrc.c file with line numbers and tabs reduced to four spaces. The output of pr is piped to the lp command with a destination of the laser printer.

DIAGNOSTICS AND BUGS

All diagnostic messages are displayed after all text has been displayed.


BSD (Berkeley)
The BSD version does not display errors when the output is to your terminal screen.

RELATED COMMANDS

Refer to the cat and pg commands described in modules 13 and 103.

RELATED FILES

The pr command reads from the standard input and writes to the standard output. It can also read from a list of files. The delay bell caused by the -f and -p options is only used when the output is directed to a /dev/tty* device. Therefore, if you pipe the output of pr to another command, the bells and pauses are suppressed.

APPLICATIONS

You can use pr to format a file for printing on a printer or displaying on your terminal screen. It can also be used to reformat the data in a file. You may want the tab stops to be reset or replace spaces with tabs where possible. The most common use of pr is probably the default listing when you don't specify any options. This causes pr to generate an output listing of your file with a header at the top of each page and a page break.

TYPICAL OPERATION

In this activity you use the pr command to display a file using the default settings, then display the same file using columns and various other options. Begin at the shell prompt.

Now for the activity using the above files.

1.  Type pr users | uniq and press Return to display the users file in the default format. The uniq command is just used to remove the multiple blank lines produced by pr.
   cj> pr users | uniq

   Mar  16  08:15  1989    users  Page 1


   Barbara
   David
   George
   Scott
   Tina
2.  Type pr -3 users and press Return to display the users file in three-column output.
   cj> pr -3 users

   Mar  16  08:16  1989    users  Page 1


   Barbara            George              Scott
   David              Jill                Tina

Notice the format of the columns. The data is displayed vertically.

System V Only

3.  Type pr -3 -a users and press Return to display the users file in three-column output with the text formatted across the columns instead of up and down. The output resembles the following listing.
   cj> pr -3 -a users

   Mar  16  08:16  1989    users  Page 1


   Barbara            David               George
   Jill               Scott               Tina
4.  Type pr -m users mgmt and press Return to display the users file merged with the mgmt file.
   cj> pr -m users mgmt

   Mar  16  08:16  1989    users  Page 1


   Barbara                Andrew
   David                  Barbara
   George                 Kathy
   Jill                   Scott
   Scott
   Tina
5.  Turn to Module 33 to continue the learning sequence.


Previous Table of Contents Next