Open In App

nl command in Linux with Examples

Linux offers a wide range of commands for text formatting and editing. While editing a text file, you might want to display the lines with line numbers appended before them, and here comes the role-play of the nl command in Linux nl command is a Unix/Linux utility that is used for numbering lines, accepting input either from a file or from STDIN. It copies each specified file to STDOUT, with line numbers appended before the lines. 

Syntax:



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

Options:

-b NUMBER or -bNUMBER : used for numbering body lines
-i NUMBER or -iNUMBER : line number increment at each line
-n FORMAT or -nNUMBER : insert line numbers according to FORMAT
-v NUMBER or -vNUMBER : change first line number of the given input 
-l NUMBER or -lNUMBER : group of NUMBER empty lines are counted as one
-s STRING or -sSTRING : add any STRING after every logical line number
-w NUMBER or -wNUMBER : use different NUMBER columns for line numbers

Examples:



Note: Consider the following file as input in all the examples.

1. To display a file with line numbers: Numbers all non-empty lines. 

$ nl geekfile.txt

2. To number all lines (including empty lines also): -b a option is used to number all lines whether empty or non-empty.

$ nl -b a geekfile.txt

3. Count multiple, consecutive, non-empty lines as one: -l option is used to count all non-empty lines as a single logical line to nl command. Linux considers NUMBER consecutive empty lines as a single logical line for numbering, and only numbers the last one. If any empty consecutive lines less than NUMBER occur, it will discard them.

$ nl -l 1 geekfile.txt 
## is same as 
## $ nl geekfile.txt

The example below considers 3 consecutive empty lines as a single logical line. 

$ nl -b a -l 3 geekfile.txt

Here, -b a option is used to consider all logical lines (whether empty or non-empty) as input to nl command.

4. Override default increment: The default increment pattern in Linux is 1. This can be changed using the -i option. The first line number is 1 and cannot be changed using -i.

$ nl -i 3 geekfile.txt
or
$ nl -i3 geekfile.txt

5. To make the starting line number different: The default line number is 1. This can be changed using the -v option.

nl -v 4 geekfile.txt
or
nl -v4 geekfile.txt

6. Add a string literal after line numbers: Any STRING literal can be added after a line number using the -s option.

$ nl -s "..." geekfile.txt

7. Change column for line numbers: Different columns can be used to display the file output using the -w option. The default column number is 1.

$ nl -w2 geekfile.txt
$ nl -w4 geekfile.txt
$ nl -w6 geekfile.txt
or
$ nl -w 2 geekfile.txt
$ nl -w 4 geekfile.txt
$ nl -w 6 geekfile.txt

Notice the change in column number (increase in indentation from left) in the example illustrated below.

8. To number all logical lines that match the specified REGEX: -b pREGEXP option can be used to append line numbers before those lines only that match the given pattern. The following command will number those lines that begin with F.

$ nl -b pF geekfile.txt

9. To print the lines using a different number format: The numbering formats can be specified using the -n option. The default numbering format is rn. The available options are:

$ nl -n ln geekfile.txt
$ nl -n rn geekfile.txt
$ nl -n rz geekfile.txt

Article Tags :