每天一个Linux命令(11)nl命令

    nl命令读取 file 参数(缺省情况下标准输入),计算输入中的行号,将计算过的行号写入标准输出。

   其默认的结果与cat -n有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐0等等的功能。

    (1)用法:

    用法:    nl  [选项]...  [文件]...

    (2)功能:

    功能:   nl命令在linux系统中用来计算文件中行号。nl 可以将输出的文件内容自动的加上行号!

    (3)选项参数:

      1) -b:                                            指定行号指定的方式,主要有两种:

               -b a:                                     表示不论是否为空行,也同样列出行号(类似 cat -n)

               -b t:                                     如果有空行,空的那一行不要列出行号(默认值) 

      2) -n:                                           列出行号表示的方法,主要有三种:

              -n ln:                                    行号在萤幕的最左方显示

              -n rn:                                    行号在自己栏位的最右方显示,且不加 0

              -n rz:                                    行号在自己栏位的最右方显示,且加 0

      3) -w:                                           行号栏位的占用的位数

      4) -p:                                           在逻辑定界符处不重新开始计算

    (4)实例:

      1)[root@localhost Documents]# nl nl_text1                     用nl列出文档中的内容,文件中的空白行不会加上行号

[root@localhost Documents]# ll 
总用量 0
[root@localhost Documents]# cat >nl_text1 <<EOF
> I am studing orders of Linux!
> I am MenAngel!
> 
> I am 19 years old!
> 
> 
> I am from AnHui HeFei!
> EOF
[root@localhost Documents]# nl nl_text1
     1    I am studing orders of Linux!
     2    I am MenAngel!
       
     3    I am 19 years old!
       
       
     4    I am from AnHui HeFei!
[root@localhost Documents]# cat -b nl_text1          //效果等于cat -b
     1    I am studing orders of Linux!
     2    I am MenAngel!

     3    I am 19 years old!


     4    I am from AnHui HeFei!

      2)[root@localhost Documents]# nl -b a nl_text1                                     用nl命令打开输出文档内容,空行也输出行号!

[root@localhost Documents]# nl -b a nl_text1
     1    I am studing orders of Linux!
     2    I am MenAngel!
     3    
     4    I am 19 years old!
     5    
     6    
     7    I am from AnHui HeFei!
[root@localhost Documents]# cat -n nl_text1                               //与cat -n具有相同的效果
     1    I am studing orders of Linux!
     2    I am MenAngel!
     3    
     4    I am 19 years old!
     5    
     6    
     7    I am from AnHui HeFei!

      3)[root@localhost Documents]# nl -b a -n rz nl_text1                            让行号前面自动补上0,统一输出格式

[root@localhost Documents]# nl -b a -n rz nl_text1
000001    I am studing orders of Linux!
000002    I am MenAngel!
000003    
000004    I am 19 years old!
000005    
000006    
000007    I am from AnHui HeFei!

      4)[root@localhost Documents]# nl -b a -n rz -w 3 nl_text1                    nl -b a -n rz命令行号默认为六位,要调整位数可以加上参数-w 3调整为3位。

[root@localhost Documents]# nl -b a -n rz -w 3 nl_text1
001    I am studing orders of Linux!
002    I am MenAngel!
003    
004    I am 19 years old!
005    
006    
007    I am from AnHui HeFei!

      5)[root@localhost Documents]# nl --help

[root@localhost Documents]# nl --help
用法:nl [选项]... [文件]...
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=样式    使用指定样式编号文件的正文行目
  -d, --section-delimiter=CC    使用指定的CC 分割逻辑页数
  -f, --footer-numbering=样式    使用指定样式编号文件的页脚行目
  -h, --header-numbering=样式    使用指定样式编号文件的页眉行目
  -i, --page-increment=数值    设置每一行遍历后的自动递增值
  -l, --join-blank-lines=数值    设置数值为多少的若干空行被视作一行
  -n, --number-format=格式    根据指定格式插入行号
  -p, --no-renumber        在逻辑页数切换时不将行号值复位
  -s, --number-separator=字符串    可能的话在行号后添加字符串
  -v, --starting-line-number=数字    每个逻辑页上的第一行的行号
  -w, --number-width=数字    为行号使用指定的栏数
      --help        显示此帮助信息并退出
      --version        显示版本信息并退出

默认的选项设置是-v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn。CC 是用于分隔
逻辑页数的两个分界符,其中缺失的第二个字符暗含了":",如果您要指定"",
请输入"\"。可用的样式如下:

  a    对所有行编号
  t    对非空行编号
  n    不编行号
  pBRE    只对符合正则表达式BRE 的行编号

FORMAT 是下列之一:

  ln    左对齐,空格不用0 填充
  rn     右对齐,空格不用0 填充
  rz     右对齐,空格用0 填充


GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告nl 的翻译错误
要获取完整文档,请运行:info coreutils 'nl invocation'

      6)[root@localhost Documents]# nl --version

[root@localhost Documents]# nl --version
nl (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本软件是自由软件:您可以自由修改和重新发布它。
在法律范围内没有其他保证。

由Scott Bartram 和David MacKenzie 编写。

    (5)其他:

    功能的详细介绍:

    在输出中,nl 命令根据您在命令行中指定的标志来计算左边的行。 输入文本必须写在逻辑页中。每个逻辑页有头、主体和页脚节(可以有空节)。 除非使用 -p 标志,nl 命令在每个逻辑页开始的地方重新设置行号。

    可以单独为头、主体和页脚节设置行计算标志(例如,头和页脚行可以被计算然而文本行不能)。

原文地址:https://www.cnblogs.com/MenAngel/p/5472794.html