Linux/Unix – Paginating Text Files with pr

The pr command provides a quick way to format a bunch of text into a form where it can be printed. This can be particularly useful if you want to print the results of some commands, without having to open up a word processor or text editor. With pr, you can format text into pages with header information such as date, time, file name, and page number.

Here is an example:

$ dpkg-query -l | sort | pr –column=2 | less — Paginate package list in 2 cols

In this example, the rpm -qa command lists all software packages installed on your system and pipes that list to the sort command, to be sorted alphabetically. Next that list is piped to the pr command, which converts the single-column list into two columns (–columns=2) and paginates it. Finally, the less command enables you to page through the text.

Instead of paging through the output, you can send the output to a file or to a printer. Here are examples of that:

$ dpkg-query -l | sort | pr –column=2 > pkg.txt   — Send pr output to a file

$ dpkg-query -l | sort | pr –column=2 | lpr   — Send pr output to printer

Other text manipulation you can do with the pr command includes double-spacing the text (-d), showing control characters (-c), or offsetting the text a certain number of spaces from the left margin (for example, -o 5 to indent five spaces from the left).


About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on March 16, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: