LinuxCommandLibrary

nl

Number lines in a file

TLDR

Number non-blank lines in a file

$ nl [path/to/file]
copy

Read from stdin
$ [command] | nl -
copy

Number [a]ll [b]ody lines including blank lines or do [n]ot number [b]ody lines
$ nl -b [a|n] [path/to/file]
copy

Number only the [b]ody lines that match a basic regular expression (BRE) [p]attern
$ nl -b p'FooBar[0-9]' [path/to/file]
copy

Use a specific [i]ncrement for line numbering
$ nl -i [increment] [path/to/file]
copy

Specify the line numbering format to [r]ight or [l]eft justified, keeping leading [z]eros or [n]ot
$ nl -n [rz|ln|rn]
copy

Specify the line numbering's [w]idth (6 by default)
$ nl -w [col_width] [path/to/file]
copy

Use a specific string to [s]eparate the line numbers from the lines (TAB by default)
$ nl -s [separator] [path/to/file]
copy

SYNOPSIS

nl [OPTION]... [FILE]...

PARAMETERS

-b STYLE
    Select which body lines to number. STYLE can be 'a' (all lines), 't' (non-empty lines), 'n' (no lines), 'p REGEX' (lines matching REGEX).

-d XY
    Use XY as the line number delimiter. X is the first delimiter, and Y is the second.

-f STYLE
    Select which footer lines to number. STYLE is interpreted like -b.

-h STYLE
    Select which header lines to number. STYLE is interpreted like -b.

-i NUMBER
    Increment at each line number (default 1).

-l NUMBER
    Count NUMBER of blank lines as one logical page (default 1).

-n FORMAT
    Line number format; FORMAT can be 'ln' (left-justified, no leading zeros), 'rn' (right-justified, no leading zeros), 'rz' (right-justified, leading zeros) (default 'rn').

-p
    Do not reset numbering at logical pages.

-s STRING
    String to use as a separator between line numbers and the corresponding text line (default TAB).

-v NUMBER
    First line number on each logical page (default 1).

-w NUMBER
    Number of columns used for line numbers (default 6).

DESCRIPTION

The nl command is a utility in Unix-like operating systems that reads lines from a file or standard input and numbers them. The command provides a flexible way to control the line numbering, including specifying the starting line number, the increment between numbers, and the format of the numbering. It is useful for generating numbered listings of code, text, or data files. The output is written to standard output and can be redirected to a file.

nl is a relatively simple command but provides a useful function when line numbers are desired for clarity or when working with files where line references are important.

CAVEATS

The nl command depends on the input file conforming to the assumptions it makes about line breaks and formatting. It may not work reliably with files that have unusual formatting or character encodings.

LOGICAL PAGES

The concept of logical pages within nl is controlled by the '-l' and '-p' options. By default, nl treats each set of blank lines as starting a new logical page, resetting the line counter. The '-p' option disables this behavior and continues the numbering across all blank lines. The '-l' option specifies how many consecutive blank lines are treated as one logical page.

HISTORY

The nl command is a standard Unix utility that has been available since the early days of Unix. It was designed to address the need for numbering lines in files, primarily for documentation and code listings. The command has seen minimal changes over the years, as its core functionality remains relevant. The original version provided basic line numbering capabilities, and subsequent versions added features like configurable line numbering styles and formatting options.

SEE ALSO

cat(1), head(1), tail(1)

Copied to clipboard