Linux Command - nl

nl - number lines of files.

Syntax

1
nl [OPTION] [FILE]...

Description

Write each FILE to standard output, with line numbers added. The default does not number blank lines.

Option Description
-b, –body-numbering=STYLE use STYLE for numbering body lines
-n, –number-format=FORMAT insert line numbers according to FORMAT
-w, –number-width=NUMBER use NUMBER columns for line numbers

-b takes a STYLE as parameter. STYLE is one of

  • a - number all lines
  • t - number only nonempty lines
  • n - number no lines

-n takes a FORMAT as parameter. FORMAT is one of

  • ln - left justified, no leading zeros
  • rn - right justified, no leading zeros
  • rz - right justified, leading zeros

Example

number lines for file1

1
nl file1

number lines including blank lines(like cat -n)

1
nl -b a file1

number lines for file1. add leading zeros to numbers

1
nl -b a -n rz file1

output

1
2
3
4
5
6
7
000001	line1
000002
000003 line3
000004
000005
000006 line6
000007

Set the number width

1
nl -b a -n rz -w 3 file1

output

1
2
3
4
5
6
7
001	line1
002
003 line3
004
005
006 line6
007

Reference