Viewing Long Files


You know how use cat to view files. But cat isn’t very satisfactory for viewing files that contain more lines than will fit on your screen. When you use cat to display a file, it prints the contents on your screen without pausing, so that long files quickly scroll past. A quick solution, when you only need to view a small part of the file, is to use cat and then hit BREAK when the part you want to read comes on the screen. This stops the program, but it leaves the output on the screen, so if your timing is good, you may get what you want.

A somewhat better solution is to use the sequence CTRL-S, to make the output pause whenever you get a screen you want to look at, and CTRL-Q to resume scrolling. This way of suspending output to the screen works for all UNIX commands, not just cat. This is still awkward, though. The best solution is to use a pager-a program that is designed specifically for viewing files.

UNIX gives you a choice of two pagers, pg and more, which are standard with all versions of UNIX, as well as an enhanced pager called less, available for many versions of UNIX, including Linux. less has more features than more and has pretty much replaced it. The following sections describe pg, mention some of the features of more, and then describe many (but not all) of the features of less.

Using pg

The pg command displays one screen of text at a time and prompts you for a command after each screen. You can use the various pg commands to move back and forth by one or more lines, by half screens, or by full screens. You can also search for and display the screen containing a particular string of text.

Moving Through a File with pg

The following command displays the file newyork one screen at a time:

 $ pg newyork

To display the next screen of text, press ENTER. To move back one page, type the hyphen or minus sign (). You can also move forward or backward several screens by typing a plus or minus sign followed by the number of screens and hitting ENTER. For example, +3 moves ahead three screens, and 3 moves back three.

You use 1 to move one or more lines forward or backward. For example, 5l moves back five lines. To move a half screen at a time, type d or press CTRL-D.

Searching for Text with pg

You can search for a particular string of text by enclosing the string between slashes. For example, the search command

 /invoices/

tells pg to display the screen containing the next occurrence of the string “invoices” in the file.

You can also search backward by enclosing the target string between question marks, as in

 ?invoices?

which scrolls backward to the preceding occurrence of “invoices” in the file.

Other pg Commands and Features

You can tell pg to show you several files in succession. The following command,

 $ pg doc.1 doc.2

shows doc.1 first; when you come to the end of it, pg shows you doc.2. You can skip from the current file to the next one by typing n at the pg prompt. And you can return to the preceding file by typing p.

The following command saves the currently displayed file with the name new_doc:

 s new_doc

To quit pg, type q or Q, or press the BREAK or DELETE key

Using pg to View the Output of a Command

You also can use pg to view the output of a command that would otherwise overflow the screen. For example, if your home directory has too many files to allow you to list them on one screen, you can send the output of ls -l to pg with this command:

 $ ls -1 pg

This is an example of the UNIX pipe feature. The pipe symbol (|) redirects the output of a command to the input of another command. It is like sending the output to a temporary file and then running the second command on that file, but it is much more flexible and convenient. Like the redirection operators, > and <, the pipe construct is a general feature of the UNIX System. Chapter 4 discusses pipes in greater detail.

Using more

UNIX has another pager, more. Like pg, more allows you to move through a file by lines, half screens, or full screens, and it lets you move backward or forward in a file and search for patterns.

To display the file newyork, just enter the command

 $ more newyork

To tell more to move ahead by a screen, press the SPACEBAR. To move ahead one line, press ENTER. The commands for half-screen motions, d and CTRL-D, are the same as in pg. To move backward by a screen, use b or CTRL-B.

Using less

The less command, an enhanced version of the more command, is a feature-rich pager that can be used to interactively display portions of a file. It can be used to move either forward or backward in a file. Since less reads in portions of files, rather than entire files, it is very efficient for large files.

This will display the file newyork with less:

 $ less newyork

Less Options

The less command has many useful options. For example, the -p option can be used to start less at the first occurrence of the pattern you specify (where the pattern is entered after the -p option) To display the file sanfrancisco, beginning with the first time the pattern “SFO" appears, you can use

 $ less -p SFO sanfrancisco

Among the other options supported by less are -s, which squeezes consecutive blank lines into a single blank line; -S, which chops off lines longer than the screen (discarding them instead of folding them into the next line); and -U, which displays backspaces and carriage returns as control characters.

Less Commands

Many commands can be used with the less pager to display different parts of a file. For example, you can scroll forward one window by entering SPACEBAR or f, and you can scroll backward one window by entering b. You can scroll forward one line by entering e or pressing ENTER, and you can scroll backward one line by entering y. You can scroll to the next occurrence of the string “pattern” using the command /pattern, and you can scroll to the preceding occurrence of this string using the command ?pattern. You can find the next and preceding lines that do not contain the string “pattern” using the commands /!pattern and ?!pattern, respectively

You can use various commands to move the cursor when using less to display files. For example, you can move one space left using the LEFT ARROW key or ESC-H; you can move one space right using the RIGHT ARROW key or ESC-L. You can move one word to the left with ESC-B and one word to the right with ESC-W. You can also do some editing: BACKSPACE deletes the character to the left of the cursor, DELETE deletes the character under the cursor, CTRL-BACKSPACE deletes the word to the left of the cursor, and CTRL-DELETE deletes the word under the cursor.

Viewing the Beginning or End of a File

You can use cat, pg, more, and less to view whole files. But often what you really want is to look at the first few lines or the last few lines of a file. For example, if a database file is periodically updated with new account information, you may want to see whether the most recent updates have been done. You also might want to check the last few lines of a file to see if it has been sorted, or read the first few lines of each of several files to see which one contains the most recent version of a note. The head and tail commands are specifically designed for these jobs.

head shows you the beginning of a file, and tail shows you the end. For example, the command shown here displays the first ten lines of transactions.

 $ head transactions

and the following command displays the last ten lines:

 $ tail transactions

To display some other number, say the last three lines, you give head or tail a numerical argument. This command shows only the last three lines:

 $ tail -3 transactions

A useful feature of tail is the -f (follow) option. This lets you use tail to check on the progress of a program that writes its output to a file. Suppose a file transfer program is getting information from a remote system and putting it in the file newdata. In this example, tail displays the last three lines of newdata, waits (sleeps) for a short time, looks to see if there has been any new input, displays any new lines, and so on:

 $ tail -3 -f newdata




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net