Print Command Line Output With `pr` Power

Print cli output with pr

The pr program provides an invaluable command line utility that paginates your formatted text output for print. Paginating your ouput can be helpful if you need to print your text results for reference, review, or some other purpose. With pr any text output you can produce on a command line can be properly formatted for print, effectively allowing you to take sublimely formatted script results and reports from your VPS server and get the results on paper, PDF, or other storage media.

Paginate Command Line Output with pr

The pr command takes a file as argument or standard input and produces a printable, paginated output. What you decide to do with this output is up to. You can pipe it to another file, send it to a printer, or save it as a PDF.

Printing Files and/or Standard Output

With the pr program, you can either print the contents of files (a single file or multiple files chained together) or standard output. This means you’re not limited to printing file contents, but you can take a file as input, perform additional operations on it, and pipe the output of those various processes to the pr command.

Also note that the results of the pr command produce yet more output and print it to your current screen. So in order to make something of the output it must be sent to a file with basic redirection or to a command like lpr which can send the output to a nearby printer.

Default Pagination Settings

The pr program provides some default headers and footers of 5 lines each and 66 lines total per page (56 lines of body text). These measurements work nicely with standard 8.5×11″ printer paper. But these parameters can be customized with various options.

Note that visual line breaks (soft wrapping by word) is not included. Lines will be cut at the columnar character limit, which may break up words or code that should otherwise remain intact.

The basic invokation of pr on a file called “mytext.txt” will produce the following output:

pr mytext.txt
2021-05-20 08:10                    mytext.txt                    Page 1


The Metamorphoses of Ovid are a compendium of the Mythological
narratives of ancient Greece and Rome, so ingeniously framed, as to
embrace a large amount of information upon almost every subject
connected with the learning, traditions, manners, and customs of
antiquity, and have afforded a fertile field of investigation to the
learned of the civilized world.

Notice the header first, following a basic pattern: date, filename, and page number. This text can be substitued by combining different options.

-D
Change the date string. -D FORMAT, or --date-format="FORMAT", replacing <FORMAT> with replacement characters like “%d” for “%Y” for year, or an arbitrary string like --date-format="Spring 2021".
-h HEADER or --header=HEADER
Replaces filename with your desired <HEADER> string, for example pr -h "My File" places “My File” in the header instead of the filename.
pr -D "Spring 2021" -h "My File" mytext.txt 
Spring 2021                        My File                        Page 1


The Metamorphoses of Ovid are a compendium of the Mythological
narratives of ancient Greece and Rome....

Some Use Cases For pr

As you can imagine, there are dozens of use cases for printing your command line output or for using pr within a larger text formatting script. Here are a few use cases that may be relevant in an office environment or for academic work.

Manuscript Documents

Before post-processing various written documents, it is considered a best practice to print and read your document in full, notating text corrections as you go. (Some would recommend reading it aloud as well.) The pr command can help you pre-format a document for print without having to copy/paste the body text into Microsoft Word or Libreoffice Writer.

Reports

With stream editing via sophisticated command line utilities like sed and awk, you can take large unformatted sets of data and produce a succinct, usable output. Pipe your scripts to pr, and you can read them like a book, distribute them to your team, or use them as supplemental material for a presentation.

Other Useful Options

Here are some useful options for pr and sample output:

Number Lines with -n

The n option allows you to number output starting with the first line of your body text. The -N option, followed by a numeric value allows you to start numbering from a digit of your choosing.

2021-05-20 08:10                    mytext.txt                    Page 1


    1	The Metamorphoses of Ovid are a compendium of the Mythological
    2	narratives of ancient Greece and Rome, so ingeniously framed, as to
    3	embrace a large amount of information upon almost every subject
    4	connected with the learning, traditions, manners, and customs of
    5	antiquity, and have afforded a fertile field of investigation to the
    6	learned of the civilized world.

Omit Header with -T

The -T option omits the header information. (This does not change the default printing of 66 lines per page.)

The Metamorphoses of Ovid are a compendium of the Mythological
narratives of ancient Greece and Rome...

If you are interested in learning about running command line operation on your server, check out the full guide on logging into your server via SSH.

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Comments

It looks like this article doesn't have any comments yet - you can be the first. If you have any comments or questions, start the conversation!

Was this article helpful? Let us know!