nl
Number lines in a file
TLDR
Number non-blank lines in a file
Read from stdin
Number [a]ll [b]ody lines including blank lines or do [n]ot number [b]ody lines
Number only the [b]ody lines that match a basic regex (BRE) [p]attern
Use a specific [i]ncrement for line numbering
Specify the line numbering format to [r]ight or [l]eft justified, keeping leading [z]eros or [n]ot
Specify the line numbering's [w]idth (6 by default)
Use a specific string to [s]eparate the line numbers from the lines (TAB by default)
SYNOPSIS
nl [OPTION]... [FILE]...
PARAMETERS
-b STYLE, --body-numbering=STYLE
Selects the numbering style for body lines. Common styles include 'a' (number all lines), 't' (number non-empty lines), 'n' (no numbering), or 'pREGEXP' (number lines matching the basic regular expression).
-h STYLE, --header-numbering=STYLE
Selects the numbering style for header lines.
-f STYLE, --footer-numbering=STYLE
Selects the numbering style for footer lines.
-l NUMBER, --line-increment=NUMBER
Sets the increment between line numbers. Defaults to 1.
-v NUMBER, --starting-line-number=NUMBER
Sets the starting line number for the first line of a logical page. Defaults to 1.
-w NUMBER, --width=NUMBER
Sets the number of characters to use for the line number. Defaults to 6.
-n FORMAT, --number-format=FORMAT
Sets the format of the line number. Options include 'ln' (left justified, no leading zeros), 'rn' (right justified, no leading zeros), and 'rz' (right justified, leading zeros). Defaults to 'rn'.
-s STRING, --separator=STRING
Sets the string used to separate the line number from the text. Defaults to a tab character.
-p, --no-renumber-at-start-of-logical-page
Do not renumber lines at the start of each logical page. This can make numbering continuous across logical pages.
-d CC, --delimiter=CC
Sets the two-character delimiter for logical page sections. The default delimiters are '\:' (for header, body, footer). For example, '-d' 'ab' would recognize 'ab' as a delimiter.
DESCRIPTION
nl is a powerful utility for numbering lines of files or standard input, writing the result to standard output. Unlike simpler numbering tools like cat -n, nl offers extensive control over the numbering process. It can number all lines, non-empty lines, or only lines matching a specific regular expression. A key feature is its ability to recognize and number lines within logical page sections – header, body, and footer – allowing for independent numbering styles for each section. This makes nl particularly useful for formatting source code, documents, or log files where precise line numbering with specific formatting or sectioning is required. It provides options to customize the starting number, increment, number width, format (left-justified, right-justified, zero-padded), and the separator between the line number and the line text, offering a high degree of flexibility for various text processing tasks.
CAVEATS
nl's primary strength lies in its ability to handle logical page numbering, which can be more complex to configure than simple line numbering tools. If only basic, continuous line numbering is needed, a simpler command like cat -n might be sufficient and easier to use. Understanding the concept of logical pages and their delimiters is crucial for leveraging nl's advanced features effectively.
LOGICAL PAGE STRUCTURE
nl processes text in logical pages, which are typically composed of three sections: header, body, and footer. These sections are identified by specific delimiters. By default, '\:\:' (two backslashes followed by two colons) marks the start of a header, '\:' (one backslash followed by one colon) marks the start of a footer, and '\:\:\:' (two backslashes followed by three colons) marks the end of a header and start of a body, or end of a body and start of a footer, depending on context. This structure allows independent numbering styles and starting numbers for different parts of a document.
NUMBERING STYLES (<I>STYLE</I>)
The STYLE argument for options like -b, -h, and -f determines how lines are numbered within a section:
- 'a': Number all lines.
- 't': Number non-empty lines (default for body).
- 'n': Do not number lines (default for header/footer).
- 'pREGEXP': Number only lines that match the basic regular expression REGEXP.
NUMBERING FORMATS (<I>FORMAT</I>)
The FORMAT argument for -n controls the line number's appearance:
- 'ln': Left-justified, no leading zeros.
- 'rn': Right-justified, no leading zeros (default).
- 'rz': Right-justified, with leading zeros.
HISTORY
The nl command is a standard utility found in Unix-like operating systems. Its roots trace back to AT&T Unix System V, providing advanced line numbering capabilities beyond what simpler tools offered. It has been a consistent part of the GNU Core Utilities, ensuring its widespread availability and ongoing maintenance in Linux distributions. Its design reflects the need for sophisticated text formatting, particularly in environments dealing with source code or structured documents.