文章目录
1. nl 命令说明
nl:显示文件内容,并列出行号。基本信息如下:
Usage: nl [OPTION]... [FILE]...
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 separating logical pages
-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 at logical pages
-s, --number-separator=STRING add STRING after (possible) line number
-v, --starting-line-number=NUMBER first line number on each logical page
-w, --number-width=NUMBER use NUMBER columns for line numbers
--help display this help and exit
--version output version information and exit
By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn. CC are
two delimiter characters for separating logical pages, a missing
second character implies :. Type \\ for \. 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
expression, BRE
FORMAT is one of:
ln left justified, no leading zeros
rn right justified, no leading zeros
rz right justified, leading zeros
Report nl bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'nl invocation'
部分参数如下:
参数 | 作用 |
---|---|
-b | 正文行号,参数为样式style |
-n | 行号格式,参数为格式format |
-w | 设定行号所占位数,参数为数字 |
-i | 每行步进量,参数为数字 |
-l | 指定将多少个连续的空行,计算为一行,参数为数字 |
-v | 以指定参数为起始行号,参数为数字 |
2. nl 命令语法
nl [选项] 文件
3. nl 命令示例
3.1 不加参数
空白行不算
nl 文件
3.2 -b(依据样式显示行号)
样式如下:
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
expression, BRE
主要是 a-显示所有行,包括空白行内容及行号;t-只显示非空白行。
3.3 -n(格式化行号)
格式化的值如下:
ln left justified, no leading zeros
rn right justified, no leading zeros
rz right justified, leading zeros
默认长度为6
3.4 -w(占位数长度)
-w 后面跟个数值,就是默认长度
举例:行号从 001 开始
nl -n rz 文件 -w 3
3.5 -i(依据数值增长行号)
-i 后面加数值,行号依据数值增长
举例:按照2个长度增加行号
nl -i 2 文件
3.6 -v(定义起始值)
-v 后面跟个数值,为行数起始值
举例:从3开始计数
nl -v 3 文件
3.7 组合使用
nl 的参数可以组合使用。
举例:从003开始,显示所有行号,包括空白行
nl -b a -n rz -w 3 -v 3 文件
4. 总结
nl 查看文件内容的同时,显示行号,可根据需求添加参数,多个参数可一起使用。