nl命令

时间:2019-08-31
本文章向大家介绍nl命令,主要包括nl命令使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

命令基础及语法

nl命令读取 file 参数(缺省情况下标准输入),计算输入中的行号,将计算过的行号写入标准输出。在输出中,nl命令根据您在命令行中指定的标志来计算左边的行。输入文本必须写在逻辑页中。每个逻辑页有头、主体和页脚节(可以有空节)。除非使用-p选项,nl 命令在每个逻辑页开始的地方重新设置行号。可以单独为头、主体和页脚节设置行计算标志(例如,头和页脚行可以被计算然而文本行不能)。其默认的结果与cat -n有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐0等等的功能。

语法

  nl (选项) (参数)

选项:

-b :指定行号指定的方式,主要有两种:
    -b a :表示不论是否为空行,也同样列出行号(类似 cat -n);
    -b t :如果有空行,空的那一行不要列出行号(默认值);

-n :列出行号表示的方法,主要有三种:
    -n ln :行号在萤幕的最左方显示;
    -n rn :行号在自己栏位的最右方显示,且不加 0-n rz :行号在自己栏位的最右方显示,且加 0-w :行号栏位的占用的位数。
-p :在逻辑定界符处不重新开始计算。

1. nl -b a 显示行数

[root@Server-n93yom tmp]# nl -b a words.txt
     1    the day is sunny the the
     2
     3    hello java
     4    he sunny is is

2. nl -n ln words.txt 行号显示在最左边,nl -n rn words.txt行号显示在右边

[root@Server-n93yom tmp]# nl -n ln words.txt
1         the day is sunny the the

2         hello java
3         he sunny is is
[root@Server-n93yom tmp]# nl -n rn words.txt
     1    the day is sunny the the

     2    hello java
     3    he sunny is is
[root@Server-n93yom tmp]#

3.nl -b a -n rz words.txt 行号补零后 行号默认为6位  -w 3为调整为3位

[root@Server-n93yom tmp]# nl -b a -n rz -w 3  words.txt
001    the day is sunny the the
002
003    hello java
004    he sunny is is

原文地址:https://www.cnblogs.com/guanbin-529/p/11439725.html