运维经–目录
nl给你的文本增加行号
nl,在Linux系统中计算文件内容行号.
nl命令 读取 file 参数(缺省情况下标准输入),计算输入中的行号,将计算过的行号写入标准输出。在输出中,nl命令根据您在命令行中指定的标志来计算左边的行。输入文本必须写在逻辑页中。每个逻辑页有头、主体和页脚节(可以有空节)。除非使用-p
选项,nl
命令在每个逻辑页开始的地方重新设置行号。可以单独为头、主体和页脚节设置行计算标志(例如,头和页脚行可以被计算然而文本行不能)。其默认的结果与cat -n
有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐0等等的功能。
使用
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
例子
1.显示文件内容 相当于 cat -n
# cat -n pwd.txt
[frank@LAPTOP-0OCJTGJR ~]$ cat -n pwd.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3
4 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5
6 adm:x:3:4:adm:/var/adm:/sbin/nologin
# nl
[frank@LAPTOP-0OCJTGJR ~]$ nl pwd.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
2.空行不显示行号-b t
[frank@LAPTOP-0OCJTGJR ~]$ nl -b t pwd.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
3.行号左对齐-n ln
[frank@LAPTOP-0OCJTGJR ~]$ nl -b t -n ln pwd.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
4.行号右对齐(默认)-n rn
[frank@LAPTOP-0OCJTGJR ~]$ nl -b t -n rn pwd.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5.行号右对齐补0(6位长)-n rz
[frank@LAPTOP-0OCJTGJR ~]$ nl -b t -n rz pwd.txt
000001 root:x:0:0:root:/root:/bin/bash
000002 bin:x:1:1:bin:/bin:/sbin/nologin
000003 daemon:x:2:2:daemon:/sbin:/sbin/nologin
000004 adm:x:3:4:adm:/var/adm:/sbin/nologin
6.行号右对齐补0(指定长度-w 2
)
[frank@LAPTOP-0OCJTGJR ~]$ nl -b t -n rz -w 2 pwd.txt
01 root:x:0:0:root:/root:/bin/bash
02 bin:x:1:1:bin:/bin:/sbin/nologin
03 daemon:x:2:2:daemon:/sbin:/sbin/nologin
04 adm:x:3:4:adm:/var/adm:/sbin/nologin