Number lines in a file

The nl command numbers, by default, all non-blank lines in a file.

Used without any options, nl numbers each non-blank line and display the result to standard output:

linux nl command

To number all lines, including empty ones, use the -b a option:

linux nl number all lines

To increment line numbers with some other value (instead of the default 1,2,3,4…), use the -i option:

linux nl increment

To add some custom string after line numbers, use the -s option:

linux nl custom string

To number only lines that match the specified regular expression, use the -b pREGEXP option. For example, to number only lines that begin with T, use the following command:

linux nl reg exp

You can specify the numbering format with the -n format option. Available formats are ln (left justified, no leading zeros), rn (right justified, no leading zeros), and rz (right justified with leading zeros):

linux nl numbering formats

Geek University 2021