linux命令大全:cat计算文件中行号

2023年 7月 11日 35.1k 0

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 列出 log.log 的内容 命令:

nl log.log

Shell

输出:

[yiibai@localhost test]$ cat log.log
this is line 1.
this is line 2.
this is line 3.
this is line 4.

this is line 5.

-----------------end
[yiibai@localhost test]$ nl log.log
     1  this is line 1.
     2  this is line 2.
     3  this is line 3.
     4  this is line 4.

     5  this is line 5.

     6  -----------------end
[yiibai@localhost test]$

Shell

说明:文件中的空白行,nl 不会加上行号。

实例二

nl 列出 log.log 的内容,空本行也加上行号。 命令:

nl -b a log.log

Shell

输出:

[yiibai@localhost test]$ nl -b a log.log
     1  this is line 1.
     2  this is line 2.
     3  this is line 3.
     4  this is line 4.
     5
     6  this is line 5.
     7
     8  -----------------end
[yiibai@localhost test]$

Shell

实例三

让行号前面自动补上0,统一输出格式。

[yiibai@localhost test]$ nl -b a -n rz log.log
000001  this is line 1.
000002  this is line 2.
000003  this is line 3.
000004  this is line 4.
000005
000006  this is line 5.
000007
000008  -----------------end
[yiibai@localhost test]$ nl -b a -n rz -w 3 log.log
001     this is line 1.
002     this is line 2.
003     this is line 3.
004     this is line 4.
005
006     this is line 5.
007
008     -----------------end
[yiibai@localhost test]$

Shell

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

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论