linux常用命令学习(十) -- nl

linux关于文件行号显示,cat / nl 命令均可以实现,但nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等等的功能。

命令格式

nl [选项] [文件]

常用命令参数

参数功能
-b-b指定行号显示有两种显示结果:-b a 无论是否为空行,均显示行号(类似 cat -n); -b -t 仅对非空行号显示,空白行不标记行号
-n-n ln 行号在行号栏最左方显示;-n rn 行号在行号栏最右方显示,且不加0;-n rz 行号在行号栏最右方显示,且加0
-w行号栏占用位数
-p在逻辑定界符处不重新开始计算

示例

  • nl -b a 无论是否为空行,均显示行号
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@kvm45072 demo1]# nl -b a demo1.log
    1 this is demo1.log
    2 this is demo1.log
    3 test
    4 test
    5
    6 test
    7
    8 demo1
    9 demo1
    10 demo
    11 d:
  • nl -b t 仅对非空行号显示,空白行不标记行号
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@kvm45072 demo1]# nl -b t demo1.log
    1 this is demo1.log
    2 this is demo1.log
    3 test
    4 test

    5 test

    6 demo1
    7 demo1
    8 demo
    9 d:
  • nl -n ln 行号在行号栏最左方显示
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@kvm45072 demo1]# nl -n ln demo1.log 
    1 this is demo1.log
    2 this is demo1.log
    3 test
    4 test

    5 test

    6 demo1
    7 demo1
    8 demo
    9 d:
  • nl -n rn 行号在行号栏最右方显示,且不加0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@kvm45072 demo1]# nl -n rn demo1.log 
    1 this is demo1.log
    2 this is demo1.log
    3 test
    4 test

    5 test

    6 demo1
    7 demo1
    8 demo
    9 d:
  • nl -n rz 行号在行号栏最右方显示,且加0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@kvm45072 demo1]# nl -n rz demo1.log 
    000001 this is demo1.log
    000002 this is demo1.log
    000003 test
    000004 test

    000005 test

    000006 demo1
    000007 demo1
    000008 demo
    000009 d:
  • nl -b a -n rz
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@kvm45072 demo1]# nl -b a -n rz demo1.log 
    000001 this is demo1.log
    000002 this is demo1.log
    000003 test
    000004 test
    000005
    000006 test
    000007
    000008 demo1
    000009 demo1
    000010 demo
    000011 d:
  • nl -b a -n rz -w 行号显示位数
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@kvm45072 demo1]# nl -b a -n rz -w 3 demo1.log 
    001 this is demo1.log
    002 this is demo1.log
    003 test
    004 test
    005
    006 test
    007
    008 demo1
    009 demo1
    010 demo
    011 d: