Linux 系统中,nl命令用于计算文件中的行号

在 Linux 系统中,nl命令用于计算文件中的行号。它可以将输出的文件内容自动加上行号,并且可以通过不同的选项来设置行号的显示方式,包括行号的位数、是否自动补齐 0 等。其命令格式为:nl(选项)…(文件)…。以下是一些常见的选项:

  • -b:指定行号指定的方式,主要有两种:
    • -b a:表示不论是否为空行,也同样列出行号(类似cat -n)。
    • -b t:如果有空行,空的那一行不要列出行号(默认值)。
  • -n:列出行号表示的方法,主要有三种:
    • -n ln:行号在屏幕的最左方显示。
    • -n rn:行号在屏幕的最右方显示,且行号从 1 开始计数。
    • -n rn:行号在屏幕的最右方显示,且行号从 0 开始计数。
nl -b t /etc/selinux/config

     1  # This file controls the state of SELinux on the system.
     2  # SELINUX= can take one of these three values:
     3  #     enforcing - SELinux security policy is enforced.
     4  #     permissive - SELinux prints warnings instead of enforcing.
     5  #     disabled - No SELinux policy is loaded.
     6  SELINUX=enforcing
     7  # SELINUXTYPE= can take one of three values:
     8  #     targeted - Targeted processes are protected,
     9  #     minimum - Modification of targeted policy. Only selected processes are protected.
    10  #     mls - Multi Level Security protection.
    11  SELINUXTYPE=targeted


 nl -n rz /etc/selinux/config

000001  # This file controls the state of SELinux on the system.
000002  # SELINUX= can take one of these three values:
000003  #     enforcing - SELinux security policy is enforced.
000004  #     permissive - SELinux prints warnings instead of enforcing.
000005  #     disabled - No SELinux policy is loaded.
000006  SELINUX=enforcing
000007  # SELINUXTYPE= can take one of three values:
000008  #     targeted - Targeted processes are protected,
000009  #     minimum - Modification of targeted policy. Only selected processes are protected.
000010  #     mls - Multi Level Security protection.
000011  SELINUXTYPE=targeted


 

以下是使用nl命令的一些示例:

  1. 基本用法:

隐藏过程

plaintext

   (root@centos79 test3)# cat a.txt
   e
   38
   i
   d
   f
   a
   e
   83
   s
   g
   g
   j
   2
   z
   t
   y
   c
   g
   s
   g
   s
   a
   (root@centos79 test3)# nl a.txt
   1 e
   2 38
   3 i
   4 d
   5 f
   6 a
   7 e
   8 83
   9 s
   10 g
   11 g
   12 j
   13 2
   14 z
   15 t
   16 y
   17 c
   18 g
   19 s
   20 g
   21 s
   22 a

  1. -n rz选项:行号在屏幕的最右方显示,且行号从 0 开始计数。

隐藏过程

plaintext

   (root@centos79 test3)# cat a.txt
   e
   38
   i
   d
   f
   a
   e
   83
   s
   g
   g
   j
   2
   z
   t
   y
   c
   g
   s
   g
   s
   a
   (root@centos79 test3)# nl -n rz a.txt
   0 e
   1 38
   2 i
   3 d
   4 f
   5 a
   6 e
   7 83
   8 s
   9 g
   10 g
   11 j
   12 2
   13 z
   14 t
   15 y
   16 c
   17 g
   18 s
   19 g
   20 s
   21 a

  1. -w选项:指定行号的宽度。

隐藏过程

plaintext

   (root@centos79 test3)# cat a.txt
   e
   38
   i
   d
   f
   a
   e
   83
   s
   g
   g
   j
   2
   z
   t
   y
   c
   g
   s
   g
   s
   a
   (root@centos79 test3)# nl -n rz a.txt
   001 e
   002 38
   003 i
   004 d
   005 f
   006 a
   007 e
   008 83
   009 s
   010 g
   011 g
   012 j
   013 2
   014 z
   015 t
   016 y
   017 c
   018 g
   019 s
   020 g
   021 s
   022 a

  1. -b选项:指定行号的显示方式。

隐藏过程

plaintext

   (root@centos79 test3)# cat a.txt
   e
   38
   i
   d
   f
   a
   e
   83
   s
   g
   g
   j
   2
   z
   t
   y
   c
   g
   s
   g
   s
   a
   (root@centos79 test3)# nl -b a a.txt
   1 e
   2 38
   3 i
   4 d
   5 f
   6 a
   7 e
   8 83
   9 s
   10 g
   11 g
   12 j
   13 2
   14 z
   15 t
   16 y
   17 c
   18 g
   19 s
   20 g
   21 s
   22 a

plaintext

   (root@centos79 test3)# cat a.txt
   e
   38
   i
   d
   f
   a
   e
   83
   s
   g
   g
   j
   2
   z
   t
   y
   c
   g
   s
   g
   s
   a
   (root@centos79 test3)# nl -b t a.txt
   1 e
   2 38
   3 i
   4 d
   5 f
   6 a
   7 e
   8 83
   9 s
   10 g
   11 g
   12 j
   13 2
   14 z
   15 t
   16 y
   17 c
   18 g
   19 s
   20 g
   21 s
   22 a
 ls | nl
     1  adjtime
     2  aliases
     3  aliases.db
     4  alternatives
     5  anacrontab
     6  asound.conf
     7  audisp
     8  audit
     9  bash_completion.d
    10  bashrc
 jps -l | nl
     1  4448 org.apache.zookeeper.server.quorum.QuorumPeerMain
     2  4622 sun.tools.jps.Jps
ps -ef | nl
     1  UID         PID   PPID  C STIME TTY          TIME CMD
     2  root          1      0  0 19:14 ?        00:00:04 /usr/lib/systemd/systemd --switched-root --s                                                                        ystem --deserialize 22
     3  root          2      0  0 19:14 ?        00:00:00 [kthreadd]
     4  root          3      2  0 19:14 ?        00:00:02 [ksoftirqd/0]
     5  root          5      2  0 19:14 ?        00:00:00 [kworker/0:0H]
     6  root          7      2  0 19:14 ?        00:00:00 [migration/0]
     7  root          8      2  0 19:14 ?        00:00:00 [rcu_bh]
     8  root          9      2  0 19:14 ?        00:00:03 [rcu_sched]
 ps aux | nl
     1  USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
     2  root          1  0.3  0.3 128168  6760 ?        Ss   19:14   0:04 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
     3  root          2  0.0  0.0      0     0 ?        S    19:14   0:00 [kthreadd]
     4  root          3  0.1  0.0      0     0 ?        S    19:14   0:02 [ksoftirqd/0]
     5  root          5  0.0  0.0      0     0 ?        S<   19:14   0:00 [kworker/0:0H]
     6  root          7  0.0  0.0      0     0 ?        S    19:14   0:00 [migration/0]
     7  root          8  0.0  0.0      0     0 ?        S    19:14   0:00 [rcu_bh]
     8  root          9  0.2  0.0      0     0 ?        S    19:14   0:03 [rcu_sched]
     9  root         10  0.0  0.0      0     0 ?        S<   19:14   0:00 [lru-add-drain]
    10  root         11  0.2  0.0      0     0 ?        S    19:14   0:03 [watchdog/0]

  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值