首页·终端命令

nl

文本处理

用法

nl [file]

一边输出一边给行编号;默认跳过空行,所以编号和编辑器不一致,加上 -b a 才对得上。

常用选项

选项含义
-b aNumber every line; -b t, the default, skips the blank ones
-wHow wide the number field is
-sWhat goes between the number and the line
-vThe first number to use
-iHow much to add per line

示例

nl -ba script.sh

Numbers every line, blank ones included.

nl -w1 -s': ' notes.txt

Compact numbering with a colon.

它们本来就是用管道串起来用的。串三条往往比让一条命令做完所有事更短。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. nl 是做什么的?

一边输出一边给行编号;默认跳过空行,所以编号和编辑器不一致,加上 -b a 才对得上。

Q. 怎么写?

nl [file] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 5 个,完整列表在 man nl。这条命令属于文本处理。

相关命令

man 手册: man nl