← 返回命令列表

Linux command

nl 命令

文件

复制后可按需替换文件名、目录或参数。

常用示例

Number non-blank lines

nl [path/to/file]

Example

[command] | nl

Example

nl -b a [path/to/file]

Example

nl -b p'[pattern]' [path/to/file]

Example

nl -i [increment] [path/to/file]

Example

nl -n [rz|ln|rn]

Example

nl -w [col_width] [path/to/file]

Example

nl -s "[separator]" [path/to/file]

说明

nl numbers lines from files or standard input. By default, it numbers only non-blank lines, preserving blank lines in the output without numbers. The command recognizes logical page sections (header, body, footer) delimited by special character sequences, allowing different numbering styles for each section. Most commonly, it's used simply to add line numbers to file contents. Output format is customizable: numbers can be left or right justified, padded with spaces or zeros, and separated from text by any string.

参数

-b, --body-numbering _style_
Numbering style: a (all), t (non-blank, default), n (none), p_regex_ (pattern)
-h, --header-numbering _style_
Numbering style for header lines (default: n)
-f, --footer-numbering _style_
Numbering style for footer lines (default: n)
-i, --line-increment _n_
Increment for line numbers (default: 1)
-l, --join-blank-lines _n_
Group of n empty lines counted as one
-n, --number-format _format_
Format: ln (left, no zeros), rn (right, no zeros, default), rz (right, with zeros)
-p, --no-renumber
Do not reset line numbers at logical pages
-w, --number-width _n_
Width of line number column (default: 6)
-s, --number-separator _string_
String between number and line (default: TAB)
-v, --starting-line-number _n_
First line number (default: 1)
-d, --section-delimiter _chars_
Characters for logical page delimiters (default: \\:)

FAQ

What is the nl command used for?

nl numbers lines from files or standard input. By default, it numbers only non-blank lines, preserving blank lines in the output without numbers. The command recognizes logical page sections (header, body, footer) delimited by special character sequences, allowing different numbering styles for each section. Most commonly, it's used simply to add line numbers to file contents. Output format is customizable: numbers can be left or right justified, padded with spaces or zeros, and separated from text by any string.

How do I run a basic nl example?

Run `nl [path/to/file]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -b, --body-numbering _style_ do in nl?

Numbering style: a (all), t (non-blank, default), n (none), p_regex_ (pattern)