Understanding the 'nl' Command (with examples)
- Linux
- December 17, 2024
The nl
command is a powerful tool in Unix-based systems used to number the lines of files or standard input (stdin
). This command is especially useful for processing text files where you want to easily identify and reference particular lines by number. The nl
command provides several options to tailor the numbering process according to your needs, allowing for a great degree of flexibility.
Use case 1: Number non-blank lines in a file
Code:
nl path/to/file
Motivation:
Numbering lines in a file is a common operation when you want to reference specific parts of a script, log, or configuration file. By default, nl
numbers only the non-blank lines, which often streamlines the viewing and editing processes by reducing the clutter and focusing on lines with actual content.
Explanation:
nl
: Invokes the line numbering command.path/to/file
: Indicates the file whose lines you wish to number.
Example Output:
1 This is the first line.
2 Here is another line.
3 And yet another line.
Use case 2: Read from stdin
Code:
command | nl -
Motivation:
When you want to number lines from the output of another command, using stdin
with the nl
command comes in handy. This is particularly helpful when processing command output on-the-fly without the need to create interim files.
Explanation:
command
: Represents any command whose output you wish to number.| nl -
: The pipe (|
) sends the output of the previous command tonl
for line numbering;-
signifies reading fromstdin
.
Example Output:
Given the command echo -e "Line one\n\nLine two" | nl -
, the output could be:
1 Line one
2 Line two
Use case 3: Number all body lines including blank lines or do not number body lines
Code:
nl --body-numbering a|n path/to/file
Motivation:
In some cases, it might be necessary to number all lines including blank ones, especially when the absence of content is significant in scripts or structured documents. Alternatively, if you wish to skip line numbering altogether in the body, you can choose not to number those lines.
Explanation:
--body-numbering
: Dictates how to handle numbering of body lines.a|n
:a
stands for numbering all lines including blank ones, whilen
stands for not numbering the body lines at all.path/to/file
: Denotes the file to operate on.
Example Output:
For nl --body-numbering a sample.txt
, where sample.txt
contains:
Line one
Line two
The output would be:
1 Line one
2
3 Line two
Use case 4: Number only the body lines that match a basic regular expression (BRE) pattern
Code:
nl --body-numbering p'FooBar[0-9]' path/to/file
Motivation:
Sometimes it is necessary to identify specific lines matching a pattern, such as lines with error codes or specific config parameters, and number them separately for attention. Using regular expressions allows for precise selection of lines needing identification.
Explanation:
--body-numbering
: Determines which lines to number.p'FooBar[0-9]'
: Number lines only if they match the given patternFooBar
followed by digits.path/to/file
: The input file to evaluate matches.
Example Output:
Given a file with lines:
Some text
FooBar1
Another line
The command would output:
1 FooBar1
Use case 5: Use a specific increment for line numbering
Code:
nl --line-increment increment path/to/file
Motivation:
Adjusting the increment for line numbering provides flexibility in scenarios where default sequential numbering is not desired. This can be useful for section numbering in documents where numbering often skips values.
Explanation:
--line-increment
: Configures the step size for line numbering.increment
: The specific step value for numbering lines.path/to/file
: Specifies the file whose lines are to be numbered.
Example Output:
For nl --line-increment 5 myfile.txt
, the output might be:
5 First line
10 Second line
15 Third line
Use case 6: Specify the line numbering format to right or left justified, keeping leading zeros or not
Code:
nl --number-format rz|ln|rn
Motivation:
Formatting line numbers in either right or left justification or with leading zeros can be aesthetically and functionally important in coding standards or documentations.
Explanation:
--number-format
: Controls the display format of line numbers.rz|ln|rn
:rz
means right-justified with leading zeros,ln
means left without zeros, andrn
right without zeros.
Example Output:
Using nl --number-format rz
, the output might appear as:
0001 Line one
0002 Line two
Use case 7: Specify the line numbering’s width
Code:
nl --number-width col_width path/to/file
Motivation:
Adjusting the width of line numbers allows for alignment in any column size, which is particularly useful for maintaining visual consistency across documents with varying amounts of content.
Explanation:
--number-width
: Sets the width of the column for line numbers.col_width
: The desired column width.path/to/file
: Indicates the target file.
Example Output:
With nl --number-width 4 example.txt
, output may be:
1 First line
2 Second line
Use case 8: Use a specific string to separate the line numbers from the lines
Code:
nl --number-separator separator path/to/file
Motivation:
Customizing the separator can enhance readability, particularly when integrating line numbers in visually distinct columns or when importing into data processing tools that require specific delimiters.
Explanation:
--number-separator
: Defines the string to separate line numbers from content.separator
: The specific separator string, such as a space or comma.path/to/file
: The input file to process.
Example Output:
Using nl --number-separator ' -> ' myfile.txt
produces:
1 -> First line
2 -> Second line
Conclusion:
The nl
command is a versatile tool for text processing and formatting, useful in various contexts from documentation to scripting. Its ability to customize number formatting according to multiple parameters ensures that it can accommodate diverse needs and preferences.