This tutorial explains Linux “nl” command, options and its usage with examples.
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.
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
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
$ 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
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.
- Apply for Programming Internship
- Check Linux Books
- Check Information Technology Books
- Practice Programming MCQs