How to Use the 'pr' Command (with Examples)
The pr
command in Unix-based systems is a powerful tool used for paginating or columnating files for printing. It offers various formatting options, making it versatile for different printing requirements. With pr
, users can easily customize headers, footers, column arrangements, and more to prepare documents for physical printing, often mimicking the layout of professionally formatted text.
Print Multiple Files with a Default Header and Footer
Code:
pr path/to/file1 path/to/file2 ...
Motivation:
When you have multiple files that you want to collate and present in a printed format with simple, consistent formatting, using pr
to add default headers and footers is highly beneficial. This is typical for quick documentation reviews or for creating hard copies of reports where the content of multiple documents needs to be considered sequentially.
Explanation:
pr
: The command to paginate files.path/to/file1 path/to/file2 ...
: This is the list of files you want to paginate and print. Each file will be formatted with a default header that includes the current date and the filename.
Example Output:
Output would include neatly formatted pages with each file’s content starting on a new page, prefixed by a header with the filename and date, and ended with a footer containing a page number.
Print with a Custom Centered Header
Code:
pr -h "Custom Header" path/to/file1 path/to/file2 ...
Motivation:
When presenting documents in a formal setting or when the document needs to be identifiable at a glance, adding a specific custom header helps communicate the context clearly. This might be useful in conferences, business meetings, or educational settings.
Explanation:
-h "Custom Header"
: The-h
flag specifies a custom header text for the paginated output, centering it at the top of each page. This text replaces the default header.path/to/file1 path/to/file2 ...
: These are the files to be printed with the specified custom header.
Example Output:
Each page of the output would have “Custom Header” centered at the top, followed by the file’s content, thus making each page easily identifiable according to its intended purpose.
Print with Numbered Lines and a Custom Date Format
Code:
pr -n -D "format" path/to/file1 path/to/file2 ...
Motivation:
Numbering lines can be crucial for reference purposes — such as academic or code review scenarios where line numbers facilitate discussion or correction. Additionally, using a custom date format ensures that the header’s date aligns with specific regional standards or personal preferences.
Explanation:
-n
: The-n
option numbers the lines in the output, making it easier to reference specific parts of the document.-D "format"
: The-D
flag allows you to specify a custom date format for the headers. Theformat
string follows typical date formatting conventions.path/to/file1 path/to/file2 ...
: These are the files to number and format according to the specified date.
Example Output:
The output would display lines with numerical prefixes for easy referencing, and each page header would present the date in the specified format, combining clarity with personalized presentation needs.
Print All Files Together, One in Each Column, without a Header or Footer
Code:
pr -m -T path/to/file1 path/to/file2 ...
Motivation:
This mode is particularly useful when comparing the content of files side-by-side or when you want to reduce page numbers by consolidating information across the page width. It’s beneficial in data entry checkpoints, coding comparisons, or when editing multiple texts simultaneously.
Explanation:
-m
: The-m
flag merges the content of the files by printing each one in a separate column on the same row.-T
: The-T
flag suppresses the header and footer, printing only the content.path/to/file1 path/to/file2 ...
: These are the files to be printed in column format.
Example Output:
The result would be all files’ contents printed horizontally across the page, showing each file’s data in its column, facilitating a streamlined comparison or review process.
Print, Beginning at Page 2 up to Page 5, with a Given Page Length
Code:
pr +2:5 -l page_length path/to/file1 path/to/file2 ...
Motivation:
Selecting specific page ranges within a document can save both paper and time during review processes or when you only need to examine a particular section of lengthy files. This is useful for standardizing reports or reviewing files with significant content that extends beyond a few pages.
Explanation:
+2:5
: This argument specifies that printing should start from page 2 and end on page 5.-l page_length
: The-l
option sets a custom page length, which includes the header and footer in the calculation.path/to/file1 path/to/file2 ...
: These files are printed starting from page 2 to page 5.
Example Output:
The output would feature only the pages from the 2nd to the 5th of the specified documents, adjusted to fit the custom-defined page length, thus optimizing the printing task.
Print with an Offset for Each Line and a Truncating Custom Page Width
Code:
pr -o offset -W width path/to/file1 path/to/file2 ...
Motivation:
Applying an offset to each line can enhance readability or conform to specific layout requirements where indentation is needed, such as technical specifications or aligned lists. By controlling the page width, users can ensure all content fits within a defined print boundary, guarding against potential text clipping during printing.
Explanation:
-o offset
: This option adds a specified number of spaces as an offset, providing indentation for each line.-W width
: Sets a maximum page width, truncating lines that exceed this width.path/to/file1 path/to/file2 ...
: Files processed for indentation and line width control.
Example Output:
Each line in the printed output appears offset by a set number of spaces and does not exceed the specified page width, maintaining consistent alignment and avoiding text overflow.
Conclusion:
The pr
command is a versatile utility in Unix-based systems, ideal for formatting files for printing with a range of customization options. From adding headers and footers to adjusting line numbers and page widths, pr offers tailored solutions to prepare documents for printed presentation. By understanding each use case, users can leverage pr
effectively for various needs, maximizing the tool’s potential to suit specific documentation or printing requirements.