TrumanWong

nl

Add line numbers to each file.

Summary

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

The main purpose

  • Add line numbers to each input file and send it to standard output.
  • When there is no file or the file is -, read standard input
  • Process logical pages.

Options

-b, --body-numbering=STYLE Use STYLE to append line numbers to lines in the body section.
-d, --section-delimiter=CC Use CC as the delimiter for logical pages.
-f, --footer-numbering=STYLE Use STYLE to append line numbers to the lines in the footer section.
-h, --header-numbering=STYLE Use STYLE to append line numbers to lines in the header section.
-i, --line-increment=NUMBER The line number increment interval is NUMBER.
-l, --join-blank-lines=NUMBER Blank lines of consecutive NUMBER lines are treated as one line.
-n, --number-format=FORMAT Insert line numbers based on FORMAT.
-p, --no-renumber Do not reset line numbers in each section.
-s, --number-separator=STRING Append the string STRING after the line number.
-v, --starting-line-number=NUMBER Starting line number for each part.
-w, --number-width=NUMBER The line number width is NUMBER.
--help Display help information and exit.
--version Display version information and exit.


The default options are: -bt -d'\:' -fn -hn -i1 -l1 -nrn -sTAB -v1 -w6

CC consists of two characters, the default is \:, if the second character is missing, the default is:

STYLE can be one of the following available values:

a All lines are marked with line numbers.
t marks line numbers only for non-empty lines.
n does not mark line numbers.
pBRE Lines matching the Basic Regular Expression (BRE) are marked with line numbers.

FORMAT can be one of the following available values:

ln is left aligned and does not add zeros at the beginning to satisfy the width.
rn is right aligned and does not add zeros at the beginning to meet the width.
rz is right aligned and will add 0 at the beginning to satisfy the width.

logical page
Composed of three parts (header, body, footer)
Start tag (header \:\:\:, body \:\:, footer \:)

Parameters

FILE (optional): The file to be processed, which can be one or more.

return value

Returning 0 indicates success, returning a non-zero value indicates failure.

example

nl_logicalpage.txt: This file is used to describe the processing of logical pages by the nl command. The content is as follows:
\:\:\:
header_1
\:\:
body_1
\:
footer_1
\:\:\:
header_2
\:\:
body_2
\:
footer_2
[user2@pc ~]$ nl nl_logicalpage.txt

        header_1

      1 body_1

        footer_1

        header_2

      1 body_2

        footer_2

[user2@pc ~]$ nl -v0 -fa -ha nl_logicalpage.txt

      0 header_1

      1 body_1

      2 footer_1

      0 header_2

      1 body_2

      2 footer_2

[user2@pc ~]$ nl -p -fa -ha nl_logicalpage.txt

      1 header_1

      2 body_1

      3 footer_1

      4 header_2

      5 body_2

      6 footer_2
nl_normal.txt: This file is used to describe the processing of normal files by the nl command. The content is as follows:
ZhuangZhu-74
2019-11-21
127.0.0.1
[user2@pc ~]$ nl nl_normal.txt
      1 ZhuangZhu-74
      2 2019-11-21
      3 127.0.0.1

[user2@pc ~]$ nl -b p'1$' nl_normal.txt
        ZhuangZhu-74
      1 2019-11-21
      2 127.0.0.1

[user2@pc ~]$ nl -b p'^[A-Z]' nl_normal.txt
      1 ZhuangZhu-74
        2019-11-21
        127.0.0.1

Notice

  1. This command is a command in the GNU coreutils package. For related help information, please see man -s 1 nl, info coreutils 'nl invocation'.