Skip to Content
gnux

nlx

Number lines of files.

Mirrors GNU nl. Run it as nlx, or as gnux nlx.

Synopsis

nlx [OPTION]... [FILE]...

GNU reference

Description

nlx writes each FILE to standard output with line numbers added. With no FILE, or when FILE is -, it reads standard input; several FILEs are numbered as one stream. By default non-empty lines are numbered from 1, right-aligned in a 6-column field followed by a tab, and empty lines get the same width of blanks so the text stays aligned. -b STYLE chooses which lines are numbered: a (all), t (non-empty, the default), n (none) or pBRE (those matching the POSIX basic regular expression BRE). -n FORMAT is ln (left-aligned), rn (right-aligned) or rz (zero-padded); -w sets the width, -s the separator, -v the first number and -i the increment (both may be zero or negative). -l N with -b a numbers only every Nth of a run of empty lines. Input may be split into logical pages of header, body and footer sections by lines holding only ':::', '::' or ':' (change the two characters with -d; an empty -d turns sections off); each delimiter prints as an empty line, -h and -f choose the numbering of headers and footers (none by default), and numbering restarts at every header unless -p. Every output line ends with a newline, a last line without one included.

Permissions

Needs read permission on each FILE and search permission on the directories leading to it. Nothing is ever modified.

Use cases

  • Quoting a snippet of code or a config file with line numbers in a review or a bug report.
  • Numbering the entries of a list, such as a playlist or a set of steps.
  • Finding the line numbers of matching lines with -b p.
  • Producing zero-padded numbers for fixed-width records with -n rz.
  • Numbering paginated documents section by section with the : delimiters.

Flags

  -b, --body-numbering STYLE          use STYLE for numbering body lines (a, t, n or pBRE; default t)
  -f, --footer-numbering STYLE        use STYLE for numbering footer lines (default n)
  -h, --header-numbering STYLE        use STYLE for numbering header lines (default n)
      --help                          display this help and exit
  -l, --join-blank-lines NUMBER       group of NUMBER empty lines counted as one (default 1)
  -i, --line-increment string         line number increment at each line (default 1)
  -p, --no-renumber                   do not reset line numbers for each section
  -n, --number-format FORMAT          insert line numbers according to FORMAT (ln, rn or rz; default rn)
  -s, --number-separator STRING       add STRING after (possible) line number (default TAB)
  -w, --number-width NUMBER           use NUMBER columns for line numbers (default 6)
  -d, --section-delimiter CC          use CC for logical page delimiters (default \:)
  -v, --starting-line-number string   first line number for each section (default 1)
      --version                       output version information and exit

Examples

Quote a config file with line numbers in a bug report

nlx nginx.conf | headx -n 5
Output
     1	user www-data;
     2	worker_processes auto;
       
     3	events {
     4	    worker_connections 768;

Number every line, empty ones included, to match an editor's line numbers

nlx -b a main.go

Find which lines of a crontab are active jobs (not comments)

nlx -b 'p^[^#]' crontab.txt

Turn a list of steps into a numbered checklist

nlx -w 2 -s '. ' steps.txt
Output
 1. Back up the database
 2. Stop the service
 3. Run the migration

Give records fixed-width, zero-padded IDs

nlx -n rz -w 5 -s , names.txt > names.csv
Output
00001,Ada
00002,Grace
00003,Linus

Continue numbering where yesterday's log left off

nlx -v 1043 today.log

Number in steps of 10, leaving room to insert lines later

nlx -i 10 -v 10 program.bas

Number a git diff's lines to discuss them in a review

git diff | nlx -b a

Number lines left-aligned with no padding before the text

nlx -n ln -w 1 -s ' ' todo.txt

Number two chapters as one continuous document

nlx chapter1.txt chapter2.txt > book.txt

Collapse runs of empty lines so only every second one gets a number

nlx -b a -l 2 poem.txt

Number lines in the portable gnux shell

gnux shell -c 'sortx names.txt | uniqx | nlx -w 3'

Options

Each option has a page of its own, with its usage and examples.

Exit status

StatusMeaning
0success: every operand was processed
1failure: an operand could not be processed (diagnosed on standard error), or a command-line mistake

Help in the terminal

nlx --help                # options, first examples, exit status
gnux help nlx             # this page: the full manual
gnux help nlx --OPTION    # one option in depth, with its examples

See also

On this page