zoukankan      html  css  js  c++  java
  • 每天一个linux命令(11):nl命令

     

    nl 命令在linux系统中用来计算文件中行号。nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等等的功能。  

    1.命令格式:

    nl [选项]... [文件]...

    2.命令参数:

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

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

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

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

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

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

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

    -w  :行号栏位的占用的位数。

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

    3.命令功能:

    nl 命令读取 File 参数(缺省情况下标准输入),计算输入中的行号,将计算过的行号写入标准输出。 在输出中,nl 命令根据您在命令行中指定的标志来计算左边的行。 输入文本必须写在逻辑页中。每个逻辑页有头、主体和页脚节(可以有空节)。 除非使用 -p 标志,nl 命令在每个逻辑页开始的地方重新设置行号。 可以单独为头、主体和页脚节设置行计算标志(例如,头和页脚行可以被计算然而文本行不能)。

    4.使用实例:

    实例一:用 nl 列出 log2012.log 的内容

    命令:

    nl log2012.log

    输出:

    [root@localhost test]# nl log2012.log 
         1  2012-01
         2  2012-02
         3  ======[root@localhost test]#

    说明:

    文件中的空白行,nl 不会加上行号

    实例二:用 nl 列出 log2012.log 的内容,空本行也加上行号

    命令:

    nl -b a log2012.log

    输出:

    [root@localhost test]# nl -b a log2012.log 
         1  2012-01
         2  2012-02
         3
         4
         5  ======[root@localhost test]#

    实例3:让行号前面自动补上0,统一输出格式

    命令:

    输出:

    [root@localhost test]# nl -b a -n rz log2014.log 
    000001  2014-01
    000002  2014-02
    000003  2014-03
    000004  2014-04
    000005  2014-05
    000006  2014-06
    000007  2014-07
    000008  2014-08
    000009  2014-09
    000010  2014-10
    000011  2014-11
    000012  2014-12
    000013  =======
    [root@localhost test]# nl -b a -n rz -w 3 log2014.log 
    001     2014-01
    002     2014-02
    003     2014-03
    004     2014-04
    005     2014-05
    006     2014-06
    007     2014-07
    008     2014-08
    009     2014-09
    010     2014-10
    011     2014-11
    012     2014-12
    013     =======

    说明:

    nl -b a -n rz 命令行号默认为六位,要调整位数可以加上参数 -w 3 调整为3位

  • 相关阅读:
    LeetCode "Jump Game"
    LeetCode "Pow(x,n)"
    LeetCode "Reverse Linked List II"
    LeetCode "Unique Binary Search Trees II"
    LeetCode "Combination Sum II"
    LeetCode "Divide Two Integers"
    LeetCode "First Missing Positive"
    LeetCode "Clone Graph"
    LeetCode "Decode Ways"
    LeetCode "Combinations"
  • 原文地址:https://www.cnblogs.com/howhy/p/6380934.html
Copyright © 2011-2022 走看看