Name

nl [options] [files] — coreutils

Synopsis

/usr/bin stdin stdout - file -- opt --help --version

nl copies its files to standard output, prepending line numbers. It’s more flexible than cat with its -n and -b options, providing an almost bizarre amount of control over the numbering. nl can be used in two ways: on ordinary text files, and on specially marked-up text files with predefined headers and footers.

Useful options

-b [a|t|n|pR]

Prepend numbers to all lines (a), nonblank lines (t), no lines (n), or only lines that contain regular expression R. (Default=a)

-v N

Begin numbering with integer N. (Default=1)

-i N

Increment the number by N for each line, so for example, you could use odd numbers only (-i2) or even numbers only (-v2 -i2). (Default=1)

-n [ln|rn|rz]

Format numbers as left-justified (ln), right-justified (rn), or right-justified with leading zeroes (rz). (Default=ln)

-w N

Force the width of the number to be N columns. (Default=6)

-s S

Insert string S between the line number and the text. (Default=TAB)

Additionally, nl has the wacky ability to divide text files into virtual pages, each with a header, body, and footer with different numbering schemes. For this to work, however, you must insert nl-specific delimiter strings into the file: \:\:\: (start of header), \:\: (start of body), and \: (start of footer). Each must appear on a line by itself. Then you can use additional options (see the manpage) to affect line-numbering in the headers and footers of your decorated file.

Get Linux Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.