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

2019-02-0919:54:53服务器及运维Comments5,373 views字数 1433阅读模式

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

1.命令格式

nl [选项]… [文件]…文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9519.html

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 命令在每个逻辑页开始的地方重新设置行号。可以单独为头、主体和页脚节设置行计算标志(例如,头和页脚行可以被计算然而文本行不能)。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9519.html

4.使用实例

实例一

nl 列出 log.log 的内容
命令:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9519.html

nl log.log
Shell

输出:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9519.html

[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 不会加上行号。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9519.html

实例二

nl 列出 log.log 的内容,空本行也加上行号。
命令:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9519.html

nl -b a log.log
Shell

输出:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9519.html

[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,统一输出格式。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9519.html

[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位。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9519.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/9519.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定