Skip to content

nl Command: Number Lines in Linux

Summary

The nl command numbers the lines of a file or standard input, offering control over the line numbering format and criteria.

Introduction

The nl command in Linux is a utility that reads lines from a file (or standard input) and numbers them. Unlike some simpler line-numbering tools, nl provides a high degree of customization over which lines are numbered and how the numbering is formatted. This makes it useful for preparing documents, scripts, or code listings where line numbers are required for reference or analysis.

Use Case and Examples

Numbering all lines in a file

nl myfile.txt
This command will number all non-empty lines in myfile.txt, prepending the line number to each line. Empty lines will not be numbered by default.

Numbering only lines containing specific text

nl -b p"pattern" myfile.txt
This command will number only lines in myfile.txt that contain the string "pattern". The -b p"pattern" option specifies that the numbering should only apply to lines matching the given regular expression.

Numbering all lines including empty lines

nl -b a myfile.txt
This command numbers all lines, including empty ones, in myfile.txt. The -b a option specifies that all lines should be numbered.

Changing the starting line number

nl -v 100 myfile.txt
This will start the numbering at 100 instead of 1 for myfile.txt.

Changing the numbering increment

nl -i 5 myfile.txt
This will number the lines with an increment of 5 (1, 6, 11, ...).

Customizing the number format

nl -n ln myfile.txt
This example changes the number format to left-justified with leading zeros using the -n ln option. Other options for -n are rn (right-justified, leading zeros) and rz (right-justified, space padding).

Changing the delimiter between number and text

nl -s ": " myfile.txt
This changes the default delimiter (usually a tab) to ": " for the line numbers in myfile.txt.

Commonly used flags

Flag Description Example
-b STYLE Specifies which lines to number, where STYLE can be a (all lines), t (non-empty lines), n (no lines), or p followed by a regular expression (lines matching the regular expression). nl -b a myfile.txt (numbers all lines)
-n FORMAT Specifies the number format, where FORMAT can be ln (left-justified, no leading zeros), rn (right-justified, leading zeros), or rz (right-justified, space padding). nl -n rn myfile.txt (right-justified numbers with leading zeros)
-v STARTNUM Sets the initial value of the line number to STARTNUM. nl -v 100 myfile.txt (starts numbering at 100)
-i INCREMENT Sets the increment value for line numbers. nl -i 5 myfile.txt (increments line numbers by 5)
-s STRING Sets the separator string used between the line number and the corresponding line text. The default is a tab. nl -s ": " myfile.txt (uses ": " as the separator)
-w WIDTH Sets the width of the line number field. Default is 6. nl -w 4 myfile.txt (uses a field width of 4 for the line numbers)


Share on Share on

Comments