Open In App

nl command in Linux with Examples

Last Updated : 11 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 Linuxnl 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]...

where,

  • OPTION: Various options for customizing the line numbering format.
  • FILE: The file(s) to be processed. If no file is specified, nl reads from the standard input.

Key Options

OptionDescription
-b NUMBER or -bNUMBERUsed for numbering body lines
-i NUMBER or -iNUMBERLine number increment at each line
-n FORMAT or -nNUMBERInsert line numbers according to the specified FORMAT (e.g., lnrnrz)
-v NUMBER or -vNUMBERChange the first line number of the given input
-l NUMBER or -lNUMBERCount a group of NUMBER consecutive empty lines as one
-s STRING or -sSTRINGAdd any STRING after every logical line number
-w NUMBER or -wNUMBERUse a different number of columns for line numbers

Examples:

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

nl command in linux

1. To display a file with line numbers:

The simplest use of the nl command is to number all non-empty lines in a file.

$ nl geekfile.txt
To display a file with line numbers

2. To number all lines (including empty lines also):

To include empty lines in the numbering, use the -b a option:

$ nl -b a geekfile.txt
To number all lines (including empty lines also)

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.

Count multiple, consecutive, non-empty lines as one

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
 Override default increment

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
To make the starting line number different:

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
Add a string literal after line numbers:

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.

Change column for line numbers

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
To number all logical lines that match the specified REGEX

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:

  • ln: left-justified, no leading zeros
  • rn: right-justified, no leading zeros
  • rz: right-justified with leading zeros
$ nl -n ln geekfile.txt
$ nl -n rn geekfile.txt
$ nl -n rz geekfile.txt
To print the lines using a different number format

Conclusion

The nl command is a powerful and versatile tool in the Linux command-line arsenal. If you're a developer, system administrator, or someone working with text files, mastering the nl command will make text processing and line numbering more efficient. With various options for customizing the output, such as setting the starting line number, changing increments, and applying regular expressions, nl can be tailored to fit a wide range of use cases.


Next Article
Practice Tags :

Similar Reads