nl

text processingLinux/Unix
The nl command is one of the most frequently used commands in Linux/Unix-like operating systems. nl Number lines of files

Quick Reference

Command Name:

nl

Category:

text processing

Platform:

Linux/Unix

Basic Usage:

nl [options] [arguments]

Common Use Cases

    Syntax

    nl [options] [file]

    Options

    Option Description
    -b, --body-numbering=STYLE Use STYLE for numbering body lines: 'a' (number all), 't' (number non-empty - default), 'n' (number none), 'p REGEXP' (number only lines matching REGEXP)
    -d, --section-delimiter=CC Use CC for separating logical pages (default is \:)
    -f, --footer-numbering=STYLE Use STYLE for numbering footer lines (same as -b)
    -h, --header-numbering=STYLE Use STYLE for numbering header lines (same as -b)
    -i, --line-increment=NUMBER Increment line number by NUMBER at each line (default 1)
    -l, --join-blank-lines=NUMBER Group NUMBER empty lines counted as one
    -n, --number-format=FORMAT Insert line numbers according to FORMAT: 'ln' (left justified, no leading zeros), 'rn' (right justified, no leading zeros), 'rz' (right justified, leading zeros)
    -p, --no-renumber Do not reset line numbers at logical pages
    -s, --number-separator=STRING Add STRING after (possible) line number (default \t)
    -v, --starting-line-number=NUMBER First line number on each logical page (default 1)
    -w, --number-width=NUMBER Use NUMBER columns for line numbers (default 6)
    --help Display help and exit
    --version Output version information and exit

    Numbering Styles:

    Style Description
    a Number all lines
    t Number only non-empty lines (default)
    n Number no lines
    p'REGEXP' Number only lines matching REGEXP

    Number Formats:

    Format Description
    ln Left justified, no leading zeros
    rn Right justified, no leading zeros
    rz Right justified, with leading zeros

    Examples

    How to Use These Examples

    The examples below show common ways to use the nl command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    Basic Examples:

    nl file.txt
    Display file.txt with line numbers added.
    nl -b a file.txt
    Number all lines in file.txt.

    Advanced Examples:

    nl -n rz -w 3 file.txt
    Number lines with right-aligned, zero-padded numbers of width 3.
    nl -v 10 file.txt Start numbering at line 10 instead of 1. nl -s '. ' file.txt Use '. ' as the separator between line numbers and text. nl -b t file.txt Number only non-empty lines (default behavior). nl -b n file.txt Do not number any lines. nl -b p'^[A-Z]' file.txt Number only lines that begin with a capital letter. nl -i 5 file.txt Increment line numbers by 5 instead of 1. nl -n ln file.txt Number lines with left-justified line numbers.
    cat file.txt | nl
    Number lines from standard input. nl -ba file1.txt file2.txt > numbered_files.txt Number all lines in multiple files and save to a new file.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The 'nl' command (short for 'number lines') is a useful text processing utility in Unix and Linux systems that adds line numbers to a file or standard input. While similar functionality can be achieved with other tools like 'cat -n', the 'nl' command provides much more control over how line numbering is performed. Key features of the nl command: 1. Selective Numbering: Unlike simpler tools, nl allows users to selectively number lines based on various criteria. By default, it only numbers non-empty lines, but it can be configured to number all lines, no lines, or only lines matching a specific pattern. 2. Customizable Format: nl offers extensive formatting options for the line numbers, including left or right justification, padding with zeros, custom width, and custom separators between the line number and text. 3. Logical Page Handling: The command recognizes the concept of logical pages in a document, divided into header, body, and footer sections. Each section can have its own numbering style. 4. Number Sequence Control: Users can specify the starting number and increment value for line numbering, allowing for various numbering schemes. 5. Pattern-Based Numbering: Using regular expressions, nl can selectively number only lines that match specific patterns, which is particularly useful for numbering sections of structured text. Common use cases for nl include: - Adding line numbers to source code for reference purposes - Numbering paragraphs or sections in documents while skipping blank lines - Creating numbered lists from plain text - Adding custom numbering to configuration files for documentation - Processing log files to add reference numbers - Creating numbered output with specific formatting requirements The nl command is particularly valuable in text processing pipelines and scripts where precise control over line numbering is required. While simple line numbering can be achieved with 'cat -n', nl is the tool of choice when more advanced numbering schemes are needed. In modern systems, nl is part of the GNU Coreutils package and is available by default on virtually all Linux distributions and other Unix-like operating systems, making it a reliable tool for cross-platform text processing tasks. When using nl, it's important to understand the distinction between its three main numbering sections (header, body, and footer), which are separated by special delimiter lines in the input. In most common usage, users only work with the body section, but the full functionality allows for complex document processing with different numbering schemes for different parts of the document.

    Related Commands

    These commands are frequently used alongside nl or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the nl command works in different scenarios.

    $ nl
    View All Commands
    Linux nl Command - Number Lines in Files