Step-by-step guide for paginating texts in Linux


How it works

While working in the Linux terminal, you may run into situations where you need to explore a large text file for a long documentation file. One way to achieve this is to paginate the text file. So in this article, we will discuss how you can perform pagination on a text file in Linux.

 

One-to-One Live Linux Classes

Coding Bootcamps school offers One-to-One Live Linux Classes for Beginners.

 

Paginating Text

The formfeed character, ASCII C-l or octal code 014, is the delimiter used to paginate text. When you send text with a formfeed character to the printer, the current page being printed is ejected and a new page begins—thus, you can paginate a text file by inserting formfeed characters at a place where you want a page break to occur.

To insert formfeed characters in a text file, use the pr filter.
Give the ‘-f’ option to omit the footer and separate pages of output with the formfeed character, and use ‘-h “”’ to output a blank header (otherwise, the current date and time, file name, and current page number are output at the top of each page).

To paginate the file ‘listings’ and write the output to a file called ‘listings.page’, type:

  $ pr -f -h "" listings >  listings.page 

By default, pr outputs pages of 66 lines each. You can specify the page length as an argument to the ‘-l’ option.

To paginate the file ‘listings’ with 43-line pages, and write the output to a file called ‘listings.page’, type:

  $ pr -f -h "" -l 43  listings > listings.page

NOTE: If a page has more lines than a printer can fit on a physical sheet of paper, it will automatically break the text at that line as well as at the places in the text where there are formfeed characters.

 

Placing Headers on Each Page

The pr tool is a general-purpose page formatter and print-preparation utility. By default, pr outputs text in pages of 66 lines each, with headers at the top of each page containing the date and time, file name, and page number, and footers containing five blank lines.
To print the file ‘duchess’ with the default pr preparation, type:

  $ pr duchess | lpr

Placing Text in Columns

You can also use pr to put text in columns—give the number of columns to output as an argument. Use the ‘-t’ option to omit the printing of the default headers and footers.
To print the file ‘news.update’ in four columns with no headers or footers, type:

  $ pr -4 -t  news.update | lpr

Options Available When Paginating Text

The following list describes some of pr’s options; see the pr info for a complete description of its capabilities.

  • +first :last: Specify the first and last page to process; the last page can be omitted, so +7 begins processing with the seventh page and continues until the end of the file is reached.
  • -column: Specify the number of columns to output text in, making all columns fit the page width.
  • -a : Print columns across instead of down.
  • -d : Specify double-spaced output
  • -h header: Specify the header to use instead of the default; specify -h “” for a blank header.
  • -l length : Specify the page length to be length lines (default 66). If page length is less than 11, headers and footers are omitted and existing form feeds are ignored.
  • -m : Use when specifying multiple files; this option merges and outputs them in parallel, one per column.
  • -o spaces : Set the number of spaces to use in the left margin (default 0).
  • -t : Omit the header and footer on each page, but retain existing
  • -T : Omit the header and footer on each page, as well as existing

Resources- Self-Paced Linux Courses

If you like to learn more about Linux, taking the following courses is highly recommended:

 

Resources- Free Courses

Here is the list of our 9 free self-paced courses that are highly recommended:

Resources- Live Linux Courses

If you like to learn more about Linux, take the following live Linux classes is highly recommended:

 

Resources- Tutorials

If you like to learn more about Linux, reading the following articles and tutorials is highly recommended: