#Bash 的 nl 命令
nl [OPTION]... [FILE]...
功能
查看文件并添加行号。
类型
可执行文件(/usr/bin/nl),属于 coreutils。
参数
OPTION选项:-b,--body-numbering=STYLE- 使用STYLE作为正文编号 风格;默认为n不编号-d,--section-delimiter=CC- 使用CC作为逻辑页的分隔符,三个分隔符表示标题,两个表示正文,一个表示页脚;默认为'\:'-f,--footer-numbering=STYLE- 使用STYLE作为页脚编号 风格;默认为n不编号-h,--header-numbering=STYLE- 使用STYLE作为标题编号 风格;默认为t对非空行编号-i,--line-increment=NUMBER- 每行编号递增NUMBER-l,--join-blank-lines=NUMBER- 将NUMBER行空行视作一行-n,--number-format=FORMAT- 行号 格式-p,--no-renumber- 新章节不重置行号-s,--number-separator=STRING- 在行号后面添加字符串STRING-v,--starting-line-number=NUMBER- 行号的初始值为NUMBER-w,--number-width=NUMBER- 行号的宽度为NUMBER--help- 显示帮助--version- 显示版本
FILE- 文件列表;如果没有这个参数或指定为-,则读取标准输入
#风格
| 风格 | 说明 |
|---|---|
a | 对所有行编号 |
t | 对非空行编号 |
n | 不编号 |
pBRE | 对正则表达式 BRE 匹配的行编号 |
#格式
| 格式 | 说明 |
|---|---|
ln | 左对齐 |
rn | 右对齐 |
rz | 右对齐,左侧补零 |
#示例
基本使用
$ cat 1.txt # 查看文件
Primers 编程伙伴
https://xplanc.org/primers/
$ nl 1.txt # 附带行号
1 Primers 编程伙伴
2 https://xplanc.org/primers/
$ nl -v 100 1.txt # 行号从 100 开始
100 Primers 编程伙伴
101 https://xplanc.org/primers/
$ nl -n ln 1.txt # 左对齐
1 Primers 编程伙伴
2 https://xplanc.org/primers/
标题和页脚
$ cat 1.txt # 查看文件
\:\:\:
Primers 编程伙伴
\:\:
Primers 致力于为各类编程学习者提供全面、系统的编程教程和实践资源。
无论你是编程新手,还是有一定基础的开发者,Primers 都提供了适合的学习路径和丰富的实战项目,帮助你从零开始,逐步掌握编程技能,成为一名优秀的开发者。
\:
https://xplanc.org/primers/
$ nl 1.txt # 默认只编号正文的非空行
Primers 编程伙伴
1 Primers 致力于为各类编程学习者提供全面、系统的编程教程和实践资源。
2 无论你是编程新手,还是有一定基础的开发者,Primers 都提供了适合的学习路径和丰富的实战项目,帮助你从零开始,逐步掌握编程技能,成为一名优秀的开发者。
https://xplanc.org/primers/
$ nl -h a -f a -b a 1.txt # 标题、页脚、正文都编号所有行
1 Primers 编程伙伴
1 Primers 致力于为各类编程学习者提供全面、系统的编程教程和实践资源。
2
3 无论你是编程新手,还是有一定基础的开发者,Primers 都提供了适合的学习路径和丰富的实战项目,帮助你从零开始,逐步掌握编程技能,成为一名优秀的开发者。
1 https://xplanc.org/primers/
$ nl -h a -f a -b a -p 1.txt # 不重置行号
1 Primers 编程伙伴
2 Primers 致力于为各类编程学习者提供全面、系统的编程教程和实践资源。
3
4 无论你是编程新手,还是有一定基础的开发者,Primers 都提供了适合的学习路径和丰富的实战项目,帮助你从零开始,逐步掌握编程技能,成为一名优秀的开发者。
5 https://xplanc.org/primers/
- 三个
\:表示标题 - 两个
\:表示正文 - 一个
\:表示页脚
#推荐阅读
#手册
NL(1) User Commands NL(1)
NAME
nl - number lines of files
SYNOPSIS
nl [OPTION]... [FILE]...
DESCRIPTION
Write each FILE to standard output, with line numbers added.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options
too.
-b, --body-numbering=STYLE
use STYLE for numbering body lines
-d, --section-delimiter=CC
use CC for logical page delimiters
-f, --footer-numbering=STYLE
use STYLE for numbering footer lines
-h, --header-numbering=STYLE
use STYLE for numbering header lines
-i, --line-increment=NUMBER
line number increment at each line
-l, --join-blank-lines=NUMBER
group of NUMBER empty lines counted as one
-n, --number-format=FORMAT
insert line numbers according to FORMAT
-p, --no-renumber
do not reset line numbers for each section
-s, --number-separator=STRING
add STRING after (possible) line number
-v, --starting-line-number=NUMBER
first line number for each section
-w, --number-width=NUMBER
use NUMBER columns for line numbers
--help display this help and exit
--version
output version information and exit
Default options are: -bt -d'\:' -fn -hn -i1 -l1 -n'rn' -s<TAB> -v1 -w6
CC are two delimiter characters used to construct logical page delim‐
iters; a missing second character implies ':'. As a GNU extension one
can specify more than two characters, and also specifying the empty
string (-d '') disables section matching.
STYLE is one of:
a number all lines
t number only nonempty lines
n number no lines
pBRE number only lines that contain a match for the basic regular ex‐
pression, BRE
FORMAT is one of:
ln left justified, no leading zeros
rn right justified, no leading zeros
rz right justified, leading zeros
AUTHOR
Written by Scott Bartram and David MacKenzie.
REPORTING BUGS
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>
COPYRIGHT
Copyright © 2023 Free Software Foundation, Inc. License GPLv3+: GNU
GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
SEE ALSO
Full documentation <https://www.gnu.org/software/coreutils/nl>
or available locally via: info '(coreutils) nl invocation'
GNU coreutils 9.4 April 2024 NL(1)