nl Command in Linux with Examples

This tutorial explains Linux “nl” command, options and its usage with examples.

nl – numbers the lines in a file.

Description :

nl copies each specified file to the standard output, with line numbers added to the lines. The line number is reset to 1 at the top of each logical page. nl treats all of the input files as a single document and does not reset line numbers or logical pages between files. A logical page consists of: header, body, and footer.

The beginnings of the sections of logical pages are indicated in the input file by a line containing nothing except one of the following delimiter strings:

\:\:\: start of header
\:\: start of body
\: start of footer

The section delimiter strings are replaced by an empty line on output. Any text that comes before the first section delimiter string in the input file is considered to be part of a body section, so a file that does not contain any section delimiter strings is considered to consist of a single body section.

advertisement
advertisement

Usage :

nl [OPTION]… [FILE]…

Options :

-b, –body-numbering=STYLE
use STYLE for numbering body lines
-d, –section-delimiter=CC
use CC for separating logical pages
-f, –footer-numbering=STYLE
use STYLE for numbering footer lines
-h, –header-numbering=STYLE
use STYLE for numbering header lines
-i, –line-increment=NUMBER
line number increment at each line
-l, –join-blank-lines=NUMBER
group of NUMBER empty lines counted as one
-n, –number-format=FORMAT
insert line numbers according to FORMAT
-p, –no-renumber
do not reset line numbers at logical pages
-s, –number-separator=STRING
add STRING after (possible) line number
-v, –starting-line-number=NUMBER
first line number on each logical page
-w, –number-width=NUMBER
use NUMBER columns for line numbers
–help
display help and exit
–version
display version information and exit

Note: Join free Sanfoundry classes at Telegram or Youtube

CC are two delimiter characters for separating logical pages. A missing second character implies a colon (:). For a backslash (\), two backslashes (\\).

STYLE is one of:
a number all lines
t number only nonempty lines
n number no lines
pBRE number only lines that contain a match for the basic regular expression, BRE

FORMAT is one of:
ln left justified, no leading zeros
rn right justified, no leading zeros
rz right justified, leading zeros

Examples :

1. A Basic Example

advertisement
$ cat list.txt
apples
oranges
potatoes
lemons
garlic
$ nl list.txt
     1 apples
     2 oranges
     3 potatoes
     4 lemons
     5 garlic

2. Save output of nl to a file

$ cat list.txt
apples
oranges
potatoes
lemons
garlic
$ nl list.txt > nltext.txt
$ cat nltext.txt
     1 apples
     2 oranges
     3 potatoes
     4 lemons
     5 garlic

Consider the following text file named text.txt for examples :

$ cat text.txt
UK
Australia
Newzealand
Brazil
America

3. Increment line numbers with any value using -i option

advertisement

The option -i can be used to override the default increment of 1 in line numbers.

Here is an example where we have used -i to increase the line number increment to 5 :

$ nl -i5 text.txt
     1 UK
     6 Australia
    11 Newzealand
    16 Brazil
    21 America

4. Add string after line numbers using -s option

By default, the nl command adds only line numbers. But, through -s option, any string can be added that can act as a separator between line numbers and the line text.

$ nl -s. text.txt
     1.UK
     2.Australia
     3.Newzealand
     4.Brazil
     5.America

5. Use a different column for line numbers using -w option

Columns for line number display can be changed using -w option.

$ nl -w1 text.txt
1 UK
2 Australia
3 Newzealand
4 Brazil
5 America
 
$ nl -w2 text.txt
 1 UK
 2 Australia
 3 Newzealand
 4 Brazil
 5 America
 
$ nl -w3 text.txt
  1 UK
  2 Australia
  3 Newzealand
  4 Brazil
  5 America
 
$ nl -w4 text.txt
   1 UK
   2 Australia
   3 Newzealand
   4 Brazil
   5 America

6. Use STYLE for numbering lines using -b option

We have used a regular expression ‘pA’ as a STYLE with option -b. This regular expression matches the lines beginning with ‘A’ and so nl command numbers only those lines.

$ nl -bpA text.txt
       UK
     1 Australia
       Newzealand
       Brazil
     2 America

7. Use different FORMAT for inserting line numbers using -n options

$ nl -nln text.txt
1      UK
2      Australia
3      Newzealand
4      Brazil
5      America
 
$ nl -nrn sort.txt
     1 UK
     2 Australia
     3 Newzealand
     4 Brazil
     5 America
 
$ nl -nrz text.txt
000001 UK
000002 Australia
000003 Newzealand
000004 Brazil
000005 America

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.