Previous Table of Contents Next

Module 103
pg (SV)


The external pg command is used to examine a file one screen at a time. It functions as an enhanced cat command or a stripped down view command, providing functionality somewhere in between. The pg paginator allows you to:

*  Peruse the standard input or multiple files.
*  Page forward or backward multiple pages.
*  Search forward or backward for a regular expression or string.
*  Scroll forward or backward by lines.
*  Goto specific lines.
*  Redisplay the current page.
*  Move forward or backward to different files.
*  Save the current file to a new file.
*  Display a built-in help screen.
*  Use options to change its functionality.


Following is the general format of the pg command.

  pg [ - ] [ -cefnrs ] [ -n ] [ -p prompt ] [ +line ] [ +/pattern/ ] \ file_list


The following list describes the options and their arguments used to control how pg functions.

- Forces pg to read from the standard input.
-c Home the cursor and clear the screen before displaying each screen. Each page is displayed on a clear screen instead of scrolling the lines. The TERM variable must be defined. The TERMinal type must have clear screen capabilities defined in the TERMCAP or TERMINFO database.
-e Don't prompt for a response at the end of each file. Normally pg prompts you at the end-of-file.
-f Inhibits line splitting. If a line is longer than the screen width, pg normally splits the line. For instance, if the line has many escape sequences to control terminal attributes, pg will split the line and it will appear incorrect. The -f option is useful when characters that don't appear on the display are embedded in the file.
-n Does not require a new-line character to process a command. Single-letter commands are interpreted and processed immediately.
-r Restrict shell escape from pg. An error message is displayed if an attempt is made to escape to the shell.
-s Turns on standout mode causing pg to display its prompts and messages in standout mode. Usually, this is reverse video. The TERMCAP or TERMINFO entry for the specified TERMinal defines what standout mode is on your terminal.
-n Specifies the number of lines pg uses for the screen window. If the terminal is defined to have a 24-line screen, the window is set to 23 lines.
-p prompt Changes the prompt displayed by pg to be prompt. Normally, a colon (:) is displayed. If %d is placed in the prompt string, the current page number is substituted.
+line Begin displaying the file at line number line.
+/pattern/ Begin displaying the file at the first occurrence of pattern. Pattern may be a string or a regular expression.


The following describes the argument that may be passed to the pg command.

file_list One or more files pg reads and displays on your terminal screen. pg prompts you between each file unless the -e option is used.


After each screen is displayed pg prompts you for a response. There are three basic types of responses:

*  Press Return to display the next screen.
*  Press Del to terminate displaying the current file. If the current file is the last file then pg terminates. Otherwise, the next file is displayed.
*  Enter a command described in the following COMMANDS section.

The pg command needs to know the attributes for your terminal. It searches the TERMCAP or TERMINFO database for the terminal defined by the TERM variable. If TERM is not set, pg assumes you are on a dumb terminal.


At the end of each screen pg prompts for a response. To continue you must enter one of the following commands at the prompt. The commands are separated into four categories:

*  Perusal commands that provide scrolling and positioning within the current file.
*  Search commands that allow you to search for a desired pattern.
*  Environment commands that let you control how pg functions.
*  Miscellaneous commands to quit, get help, save current file, or escape to a subshell.

Perusal Commands

These commands let you move around the current file (line-by-line motion only) and peruse the data. An address may be placed before these commands except for the Redisplay (. or Ctrl-L) and Last Screen ($) commands.

The address affects pages or lines depending on the command. Using these commands you can move to an absolute address or a relative address. An absolute address is measured from the beginning of the file. A relative address is measured from your current position within the file.

To move to an absolute address you precede the command with an unsigned number. For example, if you want to go to line 25 of the file you type 25l. To move to a relative address you precede the command with a signed number. For instance, +25l would move you forward 25 lines. To move backward you use -25l. The default address is a relative movement of +1 for each command. In the following descriptions adr refers to an address, the brackets ([]) signify that the address is optional.

[adr]Return Display the page specified by address adr. If you use an unsigned number like 3, pg goes to page three. If you use +3, pg moves forward three pages. And -3 causes pg to move backward three pages. If you do not provide adr, +1 is assumed and the next page is displayed.
[adr]l Scroll number of lines specified by address adr. If a number like 15 is used pg moves to line 15. If a +15 is is used pg moves forward 15 lines. And if -15 is used pg moves back 15 lines.
[adr]d Scroll half of the screen length. A positive number means to scroll
[adr]Ctrl-D forward. A negative number means scroll backward.

The following two perusal commands do not accept addresses:

. (period) Redisplay the current screen. Use this command after using the h
Ctrl-L command or if line noise has garbled your screen (if connected via a modem).
$ Go to the last screen of the current file and display it.

Search Commands

The search commands provide a means of skipping forward or backward to an occurrence of a known text pattern. The pattern may be a simple string of text or a regular expression. Regular expressions are generalized patterns that match multiple sets of text. The regular expressions used by pg are the same as defined in Module 39, which discusses the ed command.

A pattern must end with a Return, even if the -n option is used. If the pattern being searched for is not found, the next page is displayed. The pattern is designated by the pat string in the following commands. The following commands are used to perform searches:

[i]/pat/[tmb] Search forward for the ith occurrence of the pattern pat. If you do not specify a number for i, 1 is assumed. The search starts with the next screen of data. Normally, the line containing the pattern is positioned at the top of the new screen. See Line Positioning in the following section for details on using tmb.
[i]^pat^[tmb] Search backward for the ith occurrence of the pattern pat. Again, 1 is
[i]?pat?[tmb] assumed if you do not specify i. The search begins on the line above the current screen. Some terminals do not handle the question mark (?) properly; if your terminal is one of these, use the caret (^) notation.

Line Positioning    By specifying one of the t, m, or b line positioning letters you can have the line displayed in one of three positions:

t Display the line that the pattern was found on at the top of the screen.
m Display the line that the pattern was found on in the middle of the screen.
b @ @T2 = Display the line that the pattern was found on at the bottom of the screen.

The last letter specified remains in effect until a new letter is used. The t letter remains in effect until you change it to m or b.

Environment Commands

The environment commands are used to change the viewing environment of the pg command. You can skip forward or backward to different files or reset the window size by using the following commands:

[i]n Skip forward to the ith next file specified on the command line. If you do not specify i, 1 is assumed.
[i]p Skip backward to the ith previous file specified on the command line. If you do not specify i, 1 is assumed.
[i]w Set the window size to i. The next window will be displayed. If i is not given, the same window size is used and the next screen of data is displayed.

Miscellaneous Commands

The following commands are used to perform various functions while using pg.

s filename Save the input in a file named filename. The input could be a pipe; by using the s command you can save it to disk. Only the current file (input stream) is saved.
Displays a help screen. A summary of each pg command is given. To redraw the same screen of text type Ctrl-L or . (period). To display the next screen press Return.
Quit pg.
!command Escape to shell. The command is passed to the command interpreter defined by the SHELL variable. If SHELL is not set, the default shell is used. The default is usually /bin/sh.
If a QUIT or INTERRUPT signal is sent to pg while it is writing output, it stops sending output and displays the command prompt. The QUIT signal is generated by pressing Ctrl-\. The INTERRUPT signal is generated by pressing Ctrl-C or Del. If either of these signals are sent while pg is at the command prompt, pg terminates or begins displaying the next file if present. The display may become garbled if one of these signals is sent while text is being displayed.


If the terminal you are using does not handle tabs properly, the output may be formatted incorrectly.

If you are using pg as a filter with another command that changes your terminal I/O settings, the settings may not be reset properly.


Refer to the cat, grep, head, sed, and tail commands described in modules 13, 60, 63, 117, and 131, respectively.


The pg command is a filter that reads the standard input and writes to the standard output. It can also read from a list of files.

If the standard output is redirected to a file or pipe (it no longer is the terminal), pg performs much like the cat command. If there is more than one file, pg places a header at the top of each file.


The pg command provides a powerful and flexible way of viewing files larger than a screen in length. It can be used in place of the cat or view command. It provides the ability to search for strings and skip lines forward and backward. It can also move between multiple files.


In this activity you use the pg command to find your login entry in the /etc/passwd file. Begin at the shell prompt.

1.  Type pg -cns /etc/passwd /etc/group and press Return. Notice the screen is cleared and the first 23 lines of the passwd file are displayed. If your TERM variable is not defined or defined incorrectly, your screen may not be cleared.
2.  Now type /mylogin and press Return at the colon prompt. The next page displayed will contain your login entry. If your passwd file is small and your login is on the first page, the search will not cause a change.
3.  Type n and press Return at the colon prompt to display the first screen of the next file. The /etc/group file will be displayed.
4.  Type h at the colon prompt. The help summary screen is displayed. To return to the same screen type a . (period).
5.  To exit pg type q.
6.  Turn to Module 90 to continue the learning sequence.

Previous Table of Contents Next