How to use the command 'pr' (with examples)

How to use the command 'pr' (with examples)

The ‘pr’ command is a utility in GNU coreutils that is used to paginate or columnate files for printing. It formats the content of files into pages or columns, adding headers and footers if desired. This article will illustrate each of the provided use cases of the ‘pr’ command with examples.

Code:

pr file1 file2 file3

Motivation: This use case is useful when you want to print the content of multiple files in a formatted manner. By default, ‘pr’ inserts a header with the file name before each file and a footer at the end of each file.

Explanation:

  • ‘file1’, ‘file2’, ‘file3’: These are the names of the files that you want to print.

Example output:

file1
This is the content of file1.

file2
This is the content of file2.

file3
This is the content of file3.

Use case 2: Print with a custom centered header

Code:

pr -h "header" file1 file2 file3

Motivation: This use case allows you to customize the header displayed before each file. You can provide a centered header to add additional information, such as the title of the document, project name, or author.

Explanation:

  • ‘-h “header”’: This option sets the custom header text. The text will be centered at the top of each file.

Example output:

          header
file1
This is the content of file1.

          header
file2
This is the content of file2.

          header
file3
This is the content of file3.

Use case 3: Print with numbered lines and a custom date format

Code:

pr -n -D "format" file1 file2 file3

Motivation: Numbered lines can be useful for referencing specific lines in the printed output. Additionally, including a custom date format in the header can provide context about when the document was last updated.

Explanation:

  • ‘-n’: This option enables line numbering.
  • ‘-D “format”’: This option sets the custom date format for the header.

Example output:

   1:   December 31, 2022
   2:   This is the content of file1.

   3:   December 31, 2022
   4:   This is the content of file2.

   5:   December 31, 2022
   6:   This is the content of file3.

Code:

pr -m -T file1 file2 file3

Motivation: Printing files in columns without headers or footers is suitable when you want to compare the content of multiple files side by side. It provides a concise representation of the file content.

Explanation:

  • ‘-m’: This option suppresses headers and footers.
  • ‘-T’: This option prints all files together, one in each column.

Example output:

This is the content of    This is the content of    This is the content of
file1                     file2                     file3

Code:

pr +2:5 -l page_length file1 file2 file3

Motivation: This use case is useful when you want to print a specific range of pages from the files with a defined page length. It allows you to select a subset of the content for printing.

Explanation:

  • ‘+2:5’: This specifies the range of the pages to be printed, starting from page 2 and ending at page 5.
  • ‘-l page_length’: This option sets the page length, including the header and footer.

Example output:

file1
This is the content of file1.

file2
This is the content of file2.

Use case 6: Print with an offset for each line and a truncating custom page width

Code:

pr -o offset -W width file1 file2 file3

Motivation: This use case is helpful when you want to offset each line by a specific length and truncate the page width to a certain value. It allows you to customize the appearance of the printed output.

Explanation:

  • ‘-o offset’: This option sets the offset for each line, shifting it by ‘offset’ spaces.
  • ‘-W width’: This option truncates the page width to ‘width’ characters.

Example output:

   This is the content of    This is the content of    This is the content of
     file1                     file2                     file3

Conclusion:

The ‘pr’ command provides versatile options for paginating and columnating files for printing. By understanding the different use cases and their corresponding arguments, you can format and customize the printed output according to your requirements.

Related Posts

Using the tcsh command (with examples)

Using the tcsh command (with examples)

Starting an interactive shell session tcsh Motivation: Starting an interactive shell session allows you to directly interact with the tcsh command-line interface.

Read More
How to use the command standard-version (with examples)

How to use the command standard-version (with examples)

Standard-version is a command-line tool that aims to automate the versioning and changelog generation process by using SemVer (semantic versioning) and Conventional Commits.

Read More
How to use the command 'import' (with examples)

How to use the command 'import' (with examples)

The ‘import’ command is a part of ImageMagick and allows users to capture all or a portion of an X server screen and save it as an image file.

Read More