Linux基础命令[13]-nl

1. nl 命令说明

nl:显示文件内容,并列出行号。基本信息如下:

Usage: nl [OPTION]... [FILE]...
Write each FILE to standard output, with line numbers added.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -b, --body-numbering=STYLE      use STYLE for numbering body lines
  -d, --section-delimiter=CC      use CC for separating logical pages
  -f, --footer-numbering=STYLE    use STYLE for numbering footer lines
  -h, --header-numbering=STYLE    use STYLE for numbering header lines
  -i, --line-increment=NUMBER     line number increment at each line
  -l, --join-blank-lines=NUMBER   group of NUMBER empty lines counted as one
  -n, --number-format=FORMAT      insert line numbers according to FORMAT
  -p, --no-renumber               do not reset line numbers at logical pages
  -s, --number-separator=STRING   add STRING after (possible) line number
  -v, --starting-line-number=NUMBER  first line number on each logical page
  -w, --number-width=NUMBER       use NUMBER columns for line numbers
      --help     display this help and exit
      --version  output version information and exit

By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn.  CC are
two delimiter characters for separating logical pages, a missing
second character implies :.  Type \\ for \.  STYLE is one of:

  a         number all lines
  t         number only nonempty lines
  n         number no lines
  pBRE      number only lines that contain a match for the basic regular
            expression, BRE

FORMAT is one of:

  ln   left justified, no leading zeros
  rn   right justified, no leading zeros
  rz   right justified, leading zeros


Report nl bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'nl invocation'

部分参数如下:

参数作用
-b正文行号,参数为样式style
-n行号格式,参数为格式format
-w设定行号所占位数,参数为数字
-i每行步进量,参数为数字
-l指定将多少个连续的空行,计算为一行,参数为数字
-v以指定参数为起始行号,参数为数字

2. nl 命令语法

nl [选项] 文件

3. nl 命令示例

3.1 不加参数

空白行不算

nl 文件

在这里插入图片描述

3.2 -b(依据样式显示行号)

样式如下:

a number all lines
t number only nonempty lines
n number no lines
pBRE number only lines that contain a match for the basic regular
expression, BRE

主要是 a-显示所有行,包括空白行内容及行号;t-只显示非空白行。

在这里插入图片描述

3.3 -n(格式化行号)

格式化的值如下:

ln left justified, no leading zeros
rn right justified, no leading zeros
rz right justified, leading zeros

默认长度为6

在这里插入图片描述

3.4 -w(占位数长度)

-w 后面跟个数值,就是默认长度

举例:行号从 001 开始

nl -n rz 文件 -w 3

在这里插入图片描述

3.5 -i(依据数值增长行号)

-i 后面加数值,行号依据数值增长

举例:按照2个长度增加行号

nl -i 2 文件

在这里插入图片描述

3.6 -v(定义起始值)

-v 后面跟个数值,为行数起始值

举例:从3开始计数

nl -v 3 文件

在这里插入图片描述

3.7 组合使用

nl 的参数可以组合使用。

举例:从003开始,显示所有行号,包括空白行

nl -b a -n rz -w 3 -v 3 文件

在这里插入图片描述

4. 总结

nl 查看文件内容的同时,显示行号,可根据需求添加参数,多个参数可一起使用。

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值