circuit-board Retrospective: Text processing on Unix with pr

Unix has always excelled at processing text. Here's a look back at the pr command to prepare documents for printing.

Like other computer systems, early Unix printed on paper, using a typewriter-style printing device. These printers provided limited formatting options, but with clever application of Unix tools, you could prepare professional-looking documents.

One such tool was the pr tool, to prepare text documents for printing. Let's explore how to use standard Unix tools, such as the pr processor and the fmt text formatter, to prepare text files for printing on a typewriter-style printer. Note that some options are GNU extensions and may not work the same on every Unix-like system.

Printing a plain text file

Let's say we wanted to print the first paragraph from the Creative Commons Attribution-ShareAlike 4.0 International Public License. This file is already formatted for optimal screen display; lines are almost 80 columns wide, which fits well on a standard terminal.

$ cat cc-by-sa.txt 
By exercising the Licensed Rights (defined below), You accept and
agree to be bound by the terms and conditions of this Creative Commons
Attribution-ShareAlike 4.0 International Public License ("Public
License"). To the extent this Public License may be interpreted as a
contract, You are granted the Licensed Rights in consideration of Your
acceptance of these terms and conditions, and the Licensor grants You
such rights in consideration of benefits the Licensor receives from
making the Licensed Material available under these terms and conditions.

Printer paper is 85 columns wide, at least on classic typewriter-like printers, which usually printed at 10 characterse per inch. So you can also print the file to a printer using a command like lpr. But that's not a very interesting way to print the file, and it won't be very nice to read. For example, the document will start on the first line of the printed page, and immediately on the left edge of the paper.

You can make the document easier to read by using the pr command to add a top margin. By default, pr includes the date and time, the name of the file, and the page number in the top header. For example, the top of your file might look like this:

$ pr cc-by-sa.txt | head


2023-05-09 17:37                   cc-by-sa.txt                   Page 1


By exercising the Licensed Rights (defined below), You accept and
agree to be bound by the terms and conditions of this Creative Commons
Attribution-ShareAlike 4.0 International Public License ("Public
License"). To the extent this Public License may be interpreted as a
contract, You are granted the Licensed Rights in consideration of Your

In this example, I've used the head command to look at just the first ten lines of the pr output. The pr command also adds extra blank lines at the bottom of the page, to provide a bottom margin. The old-style typewriter printers used 66 lines per page, so the pr output assumes that too. But this file prints on one page, so I don't need to show the bottom of the file; it's just some blank lines.

Set width and add a margin

Adding the top margin makes the document easier to read, but you can do better by adding space on the left and right of the printed page. This effectively adds a left and right margin to your document.

The first step is to use the fmt command to reformat the text file to a different width. Using fmt -w 70 reformats the text file to use lines that are 70 columns wide. You can add some blank space on the left of the document to create a left margin. Using pr -o 5 adds 5 spaces to the start of each line of the output. With the narrower text, you will also have about 5 spaces in the right margin.

$ fmt -w 70 cc-by-sa.txt | pr -o 5 | head
   

    2023-05-09 17:39                                                  Page 1


    By exercising the Licensed Rights (defined below), You accept and
    agree to be bound by the terms and conditions of this Creative
    Commons Attribution-ShareAlike 4.0 International Public License
    ("Public License"). To the extent this Public License may be
    interpreted as a contract, You are granted the Licensed Rights in

You can use the same set of commands to print text files on a modern laser printer, but your printer may expect a page feed command instead of using blank lines. To do that, add the -f option to the pr command, like this:

$ fmt -w 70 cc-by-sa.txt | pr -f -o 5 | lpr

I'll omit the -f in the rest of this article, but remember to add -f to the pr command if you want to print documents to a modern laser printer.

Set the page header

When you redirect the output of fmt to the pr command, the pr output no longer shows the name of the file. That's because when you chain several commands together like this, the pr command doesn't know the filename, so it's left blank. You can add the document title to the header by using the -h option:

$ fmt -w 70 cc-by-sa.txt | pr -h 'CC By-SA' -o 5 | head
   

    2023-05-09 17:40                     CC By-SA                     Page 1


    By exercising the Licensed Rights (defined below), You accept and
    agree to be bound by the terms and conditions of this Creative
    Commons Attribution-ShareAlike 4.0 International Public License
    ("Public License"). To the extent this Public License may be
    interpreted as a contract, You are granted the Licensed Rights in

You can make other changes to the header, such as the -D option to change the date and time format, or replace it with new text.

$ fmt -w 70 cc-by-sa.txt | pr -D 'May 9, 2023' -h 'CC By-SA' -o 5 | head
   

    May 9, 2023                       CC By-SA                        Page 1


    By exercising the Licensed Rights (defined below), You accept and
    agree to be bound by the terms and conditions of this Creative
    Commons Attribution-ShareAlike 4.0 International Public License
    ("Public License"). To the extent this Public License may be
    interpreted as a contract, You are granted the Licensed Rights in

Printing two columns

What if you wanted to make a text document look really fancy on the printed page? Certain documents such as technical articles might need to be printed in a two-column layout. The pr command can print text in multiple columns. For example, -2 prints in two columns and -3 will print in three columns.

However, be careful when printing text in multiple columns. If the lines are too long, pr may simply overlap one column with another, effectively losing text in the output. But you can leverage the fmt command to reformat the text to a narrower width, suitable for printing in two column format.

Let's do the math: If the printed page is 85 columns wide, and with 5 spaces on the left and right as page margins, that leaves 75 columns for the text. Using fmt -w 35 would divide the text almost in half for two columns, but will not leave much space between the columns. Instead, let's use fmt -w 33 to reformat the text width to 33 before sending the output to the pr command:

$ fmt -w 33 cc-by-sa.txt | pr -2 -D 'May 9, 2023' -h 'CC By-SA' -o 5 | head
   

    May 9, 2023                       CC By-SA                        Page 1


    By exercising the Licensed         You are granted the Licensed
    Rights (defined below), You     Rights in consideration of Your
    accept and agree to be bound     acceptance of these terms and
    by the terms and conditions     conditions, and the Licensor
    of this Creative Commons         grants You such rights in

Unix is a great platform for processing text. While we use other tools today, including HTML in web browsers and PDF for printable content, the command line tools can create professional plain text documents.