nl

nl linux command cheatsheet by Thamizhiniyan C S

Introduction

The nl command is used to number lines of files.


Syntax

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


Important Flags

FlagsDescription

-b STYLE

Used for numbering body lines

-i NUMBER

Line number increment at each line

-n FORMAT

Insert line numbers according to FORMAT

-v NUMBER

Change first line number of the given input

-l NUMBER

Group of NUMBER empty lines are counted as one

-s STRING

Add any STRING after every logical line number

-w NUMBER

Use different NUMBER columns for line numbers

Default Options

-b t -d ':' -f n -h n -i 1 -l 1 -n 'rn' -s -v 1 -w 6

STYLE to be used with Options

ValuesDescription

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 to be used with Options

FlagsDescription

ln

Left justified, no leading zeros

rn

Right justified, no leading zeros

rz

Right justified, leading zeros


Examples

CommandDescription
nl readme.md

To display a file with line numbers (Numbers all non-empty lines)

nl -b a readme.md

To number all lines (including empty lines also)

nl -b a -l 3 readme.md

Count multiple, consecutive, non-empty lines as one

nl -i 3 readme.md

Override default increment

nl -v 4 readme.md

To make the starting line number different

nl -s "..." readme.md

Add a string literal after line numbers

nl -w 6 readme.md

Change column for line numbers

nl -b pF readme.md

To number all logical lines that match the specified REGEX (number those lines that begin with F)

nl -n ln readme.md

To print the lines using left-justified, no leading zeros number format

nl -n rn readme.md

To print the lines using right-justified, no leading zeros number format

nl -n rz readme.md

To print the lines using right-justified with leading zeros format

Last updated