O'Reilly logo

Linux Pocket Guide, 2nd Edition by Daniel J. Barrett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

nl — stdin  stdout  - file  -- opt  --help  --version

Synopsis

nl [options] [files]

nl copies its files to standard output, prepending line numbers.

$ nl myfile
     1  Once upon a time, there was
     2  a little operating system named
     3  Linux, which everybody loved.

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|p R ]

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, such as \:\:\: (start of header), \:\: (start of body), and \: (start of footer). Each must appear on a line by itself. Then ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required