linux nl命令详解


nl命令显示文件的内容,nl可以将输出的文件内容自动加上行号,默认的结果类似与cat -n。 但是nl可以将行号做比较多的显示设计,包括位数与是否自动补齐0等的功能。
nl可以将文件内容添加行号打印出来,类似cat命令的-n和-b选项,但是nl可以对行号字段做更多的显示设计。

nl命令常用格式如下:
            [root@initroot ~]# nl [-bnw] 文件
            
选项与参数:
-b :指定行号指定的方式,主要有两种:
-b a :类似cat -n,表示不论是否为空行,都列出行号;
-b t :类似cat -n,只列出非空行的行号,此选项为nl的默认选项;
-n:列出行号表示的方法,主要有三种:
-n ln :行号在行号字段的最左方显示;
-n rn :行号在行号字段的最右方显示,且不加0;
-n rz :行号在行号字段的最右方显示,且加0;
-w :行号字段占用的字符数。
光看说明解释还是有点看不懂的,这时候打开terminal工具,亲自操作一下就可以了。 用nl命令显示出/etc/issue文件的内容:
            [root@initroot ~]# nl /etc/issue
            1	Linux Mint 19.3 Tricia \n \l
            
            
最后一行为空行,nl默认不显示空白行的行号,如果要显示所有行的行号,加上-b a选项即可:
            [root@initroot ~]# nl -b a /etc/issue
            1	Linux Mint 19.3 Tricia \n \l
            2	
            
在行号前面自动补上0:
            [root@initroot ~]# nl -b a -n rz /etc/issue
            000001	Linux Mint 19.3 Tricia \n \l
            000002	
            
我们看到行号字段默认是六位数,可通过-w选项设置行号字段的字符位数, 将行号字段字符位数设置为3:
            [root@initroot ~]# nl -b a -n rz -w 3 /etc/issue
            001	Linux Mint 19.3 Tricia \n \l
            002	
            
这就是nl无聊的地方了,真是术业有专攻,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 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

                By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn.
                CC are two delimiter characters used to construct logical page delimiters,
                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


                GNU coreutils online help: http://www.gnu.org/software/coreutils/ Full documentation at:
                http://www.gnu.org/software/coreutils/nl or available locally via: info '(coreutils) nl invocation'
              

initroot编辑整理,转载请注明www.initroot.com

100次点赞 100次阅读