nl is a Unix utility for numbering lines, either from a file or from standard input, reproducing output on standard output.

History

nl is part of the X/Open Portability Guide since issue 2 of 1987. It was inherited into the first version of POSIX.1 and the Single Unix Specification.[1] It first appeared in System V release 2.[2]

The version of nl bundled in GNU coreutils was written by Scott Bartram and David MacKenzie.[3]

The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities.[4]

Syntax

The command has a number of switches:

  • a - number all lines
  • t - number lines with printable text only
  • n - no line numbering
  • pstring - number only those lines containing the regular expression defined in the string supplied.

The default applied switch is t.

nl also supports some command line options.

Example

 $ nl tf
     1  echo press cr
     2  read cr
     3  done

The following example numbers only the lines that begin with a capital letter A (matching on the regular expression /^A/). filename is optional.

$ nl -b p^A filename
       apple
    1  Apple
       BANANA
    2  Allspice
       strawberry

It can be useful as an alternative to grep -n:

$ cat somefile
aaaa
bbbb
cccc
dddc
$ nl -ba somefile | grep cccc
    3 cccc

See also

References

  1. nl  Shell and Utilities Reference, The Single UNIX Specification, Version 5 from The Open Group
  2. nl(1)  FreeBSD General Commands Manual
  3. nl(1)  Linux General Commands Manual from ManKier.com
  4. "Native Win32 ports of some GNU utilities". unxutils.sourceforge.net. Archived from the original on 2006-02-09. Retrieved 2025-08-09.