cmd line tutorials - gencmd

cmd line tutorials - gencmd

The nl Command

Unix-Linux Mac

The nl command prints the specified file to stdout with line numbers appended before the lines. It is a useful tool for a variety of tasks, such as:

  • Extracting the line numbers of a file.
  • Adding line numbers to a file before printing it.
  • Generating sequential numbers for a list of items.

Syntax

The basic syntax for the nl command is as follows:

nl [options] <file>

The <file> argument is the path to the file that you want to print with line numbers. If you do not specify a file, the nl command will read from STDIN.

The [options] argument is a list of options that can be used to modify the behavior of the nl command.

Examples

Print the file “myfile.txt” with line numbers.

nl myfile.txt

Print the file “myfile.txt” with line numbers for all lines.

nl -ba myfile.txt

Print the file “myfile.txt” with line numbers for non-empty lines.

nl -bt myfile.txt

Start numbering at 10.

nl -v 10 myfile.txt

With gencmd

gencmd -c nl number lines of a file

  • nl file.txt