0018. shell命令--nl

目录

18. shell命令--nl

功能说明

语法格式

选项说明

实践操作

注意事项


18. shell命令--nl

功能说明

        nl 是 Linux 中的一个实用程序,用于给文件的每一行加上行号。它通常用于查看或打印带有行号的文件内容。nl命令来自英文词组“Number of lines”的缩写,其功能是用于显示文件内容及行号,使用nl命令会有类似于“cat -n 文件名”的效果,除此之外还可以对于显示的行号格式进行深度定制。

语法格式

nl [选项] [文件...]

SYNOPSIS
       nl [OPTION]... [FILE]... 

选项说明

  • -b:指定行号指定的方式。
    • t:只对非空行添加行号。
    • a:对所有行(包括空行)添加行号。
    • n:对没有空字符的行添加行号(默认)。
    • p:对逻辑页(使用 formfeed 字符分隔的行)的第一行添加行号。
    • s:只对每个逻辑页的第一行添加行号(与 -p 类似,但只打印行号,不打印内容)。
    • j:对每个逻辑页的所有行添加行号(与 -p 类似,但打印所有行)。
  • -d:指定分隔符,用于在行号和内容之间插入。默认为制表符。
  • -f:指定行号的格式。
    • ln:左对齐,没有前导零(默认)。
    • rn:右对齐,没有前导零。
    • rz:右对齐,有前导零。
    • lz:左对齐,有前导零。
  • -i:指定行号的增量。例如,-i 2 意味着每两行增加一次行号。
  • -l:指定最大的行数(当处理大文件时有用)。
  • -n:指定行号的格式。
    • ln:左对齐,没有前导零(默认)。
    • rn:右对齐,没有前导零。
    • rz:右对齐,有前导零。
    • lz:左对齐,有前导零。
    • wn:行号前没有空格,且行号字段宽度为 n 个字符。
    • bn:在行号前后都加上空格,且行号字段宽度为 n 个字符。
  • -p:逻辑页(与 -b p 类似)。
  • -s:在行号和内容之间插入指定的字符串(与 -d 类似,但 -s 允许更长的字符串)。
  • -v:设置开始行号。例如,-v 10 意味着从 10 开始编号。
  • -w:设置行号字段的宽度。

实践操作

1. 显示指定文件内容及行号信息
nl /root/.bashrc

2. 显示指定文件内容及行号信息,空行也加上行号
nl -b a /root/.bashrc

3. 空行也算一行,并且行号前面自动补0,统一输出格式后显示指定文件内容及行号信息
nl -b a -n rz /root/.bashrc

4. 设置行号字段的宽度为 N 个字符,并右对齐
nl -n rz -w 2 /root/.bashrc
nl -n rz -w 5 /root/.bashrc

5. 为非空行添加行号,并设置开始行号为 100
nl -b t -v 100 /root/.bashrc

6. 使用自定义的分隔符(如顿号)添加行号
nl -s '、' /root/.bashrc

7. 与其它符号或命令结合使用
nl /etc/ssh/sshd_config | less
grep "$PS1" /etc/bashrc
nl /etc/bashrc | grep "$PS1"

命令示例:1. 显示指定文件内容及行号信息

nl /root/.bashrc

输出结果:

[root@MineGi ~]# nl /root/.bashrc
     1    # .bashrc
       
     2    # User specific aliases and functions
       
     3    alias rm='rm -i'
     4    alias cp='cp -i'
     5    alias mv='mv -i'
       
     6    # Source global definitions
     7    if [ -f /etc/bashrc ]; then
     8        . /etc/bashrc
     9    fi
[root@MineGi ~]#

命令示例:2. 显示指定文件内容及行号信息,空行也加上行号

nl -b a /root/.bashrc

输出结果:

[root@MineGi ~]# nl -b a /root/.bashrc
     1    # .bashrc
     2    
     3    # User specific aliases and functions
     4    
     5    alias rm='rm -i'
     6    alias cp='cp -i'
     7    alias mv='mv -i'
     8    
     9    # Source global definitions
    10    if [ -f /etc/bashrc ]; then
    11        . /etc/bashrc
    12    fi
[root@MineGi ~]# 

命令示例:3. 空行也算一行,并且行号前面自动补0,统一输出格式后显示指定文件内容及行号信息

nl -b a -n rz /root/.bashrc

输出结果:

[root@MineGi ~]# nl -b a -n rz /root/.bashrc
000001    # .bashrc
000002    
000003    # User specific aliases and functions
000004    
000005    alias rm='rm -i'
000006    alias cp='cp -i'
000007    alias mv='mv -i'
000008    
000009    # Source global definitions
000010    if [ -f /etc/bashrc ]; then
000011        . /etc/bashrc
000012    fi
[root@MineGi ~]#

命令示例:4. 设置行号字段的宽度为 N 个字符,并右对齐

nl -n rz -w 2 /root/.bashrc
nl -n rz -w 5 /root/.bashrc

输出结果:

[root@MineGi ~]# nl -n rz -w 2 /root/.bashrc
01    # .bashrc
   
02    # User specific aliases and functions
   
03    alias rm='rm -i'
04    alias cp='cp -i'
05    alias mv='mv -i'
   
06    # Source global definitions
07    if [ -f /etc/bashrc ]; then
08        . /etc/bashrc
09    fi
[root@MineGi ~]# nl -n rz -w 5 /root/.bashrc
00001    # .bashrc
      
00002    # User specific aliases and functions
      
00003    alias rm='rm -i'
00004    alias cp='cp -i'
00005    alias mv='mv -i'
      
00006    # Source global definitions
00007    if [ -f /etc/bashrc ]; then
00008        . /etc/bashrc
00009    fi
[root@MineGi ~]# 

命令示例:5. 为非空行添加行号,并设置开始行号为 100

nl -b t -v 100 /root/.bashrc

输出结果:

[root@MineGi ~]# nl -b t -v 100 /root/.bashrc
   100    # .bashrc
       
   101    # User specific aliases and functions
       
   102    alias rm='rm -i'
   103    alias cp='cp -i'
   104    alias mv='mv -i'
       
   105    # Source global definitions
   106    if [ -f /etc/bashrc ]; then
   107        . /etc/bashrc
   108    fi
[root@MineGi ~]# 

命令示例:6. 使用自定义的分隔符(如顿号)添加行号

nl -s '、' /root/.bashrc

输出结果:

[root@MineGi ~]# nl -s '、' /root/.bashrc
     1、# .bashrc
         
     2、# User specific aliases and functions
         
     3、alias rm='rm -i'
     4、alias cp='cp -i'
     5、alias mv='mv -i'
         
     6、# Source global definitions
     7、if [ -f /etc/bashrc ]; then
     8、    . /etc/bashrc
     9、fi
[root@MineGi ~]#

命令示例:7. 与其它符号或命令结合使用

nl /etc/ssh/sshd_config | less
grep -n "\$PS1" /etc/bashrc
nl /etc/bashrc | grep "$\PS1"

输出结果:

[root@MineGi ~]# nl /etc/ssh/sshd_config | less
[root@MineGi ~]# grep -n "\$PS1" /etc/bashrc
12:if [ "$PS1" ]; then
41:  #[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
42:  [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\] \[\e[31;1m\]\w\[\e[0m\]]\\$ "
46:  # if [ "$PS1" ]; then
82:            if [ "$PS1" ]; then
[root@MineGi ~]# nl /etc/bashrc | grep "$\PS1"
     9    if [ "$PS1" ]; then
    38      #[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
    39      [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\] \[\e[31;1m\]\w\[\e[0m\]]\\$ "
    43      # if [ "$PS1" ]; then
    76                if [ "$PS1" ]; then
[root@MineGi ~]#

注意事项

  • 在使用 nl 命令时,要注意文件的大小和行数。如果文件非常大,包含数百万或数千万行,那么 nl 命令可能会消耗大量的内存和 CPU 时间。在这种情况下,你可以考虑使用 -l 选项来限制 nl 命令处理的行数。
  • 另外,虽然 nl 命令可以很方便地为文件添加行号,但如果你需要频繁地对文件进行这种操作,或者需要在多个文件中保持一致的行号格式,那么你可能需要考虑使用更强大的文本处理工具或编程语言来实现这些功能。

nl 是 Linux 中一个非常实用的命令,它可以帮助你快速地为文件的每一行添加行号。通过掌握 nl 命令的基本用法和常用选项,你可以更高效地处理文本文件,并在需要时轻松地查看或打印带有行号的文件内容。如果你对 nl 命令还有其他疑问或需要了解更多高级用法,建议查阅其 man 页面或相关文档。

[root@yfw ~]# cd /opt/openfire [root@yfw openfire]# ls -R /opt/openfire/plugins/restapi/ /opt/openfire/plugins/restapi/: changelog.html i18n lib logo_large.gif logo_small.gif plugin.xml readme.html web /opt/openfire/plugins/restapi/i18n: restapi_i18n_nl.properties restapi_i18n_pt_PT.properties restapi_i18n_uk_UA.properties restapi_i18n.properties restapi_i18n_ru_RU.properties restapi_i18n_zh_CN.properties /opt/openfire/plugins/restapi/lib: aopalliance-repackaged-2.6.1.jar jersey-common-2.45.jar classgraph-4.8.179.jar jersey-container-jetty-http-2.45.jar commons-lang3-3.17.0.jar jersey-container-jetty-servlet-2.45.jar hk2-api-2.6.1.jar jersey-container-servlet-2.45.jar hk2-locator-2.6.1.jar jersey-container-servlet-core-2.45.jar hk2-utils-2.6.1.jar jersey-entity-filtering-2.45.jar jackson-annotations-2.17.1.jar jersey-hk2-2.45.jar jackson-core-2.17.1.jar jersey-media-jaxb-2.45.jar jackson-databind-2.17.1.jar jersey-media-json-jackson-2.45.jar jackson-dataformat-yaml-2.18.2.jar jersey-server-2.45.jar jackson-datatype-jsr310-2.18.2.jar osgi-resource-locator-1.0.3.jar jackson-jaxrs-base-2.18.2.jar restAPI-1.12.0.jar jackson-jaxrs-json-provider-2.18.2.jar slf4j-api-2.0.9.jar jackson-module-jaxb-annotations-2.17.1.jar snakeyaml-2.3.jar jakarta.annotation-api-1.3.5.jar swagger-annotations-2.2.31.jar jakarta.inject-2.6.1.jar swagger-core-2.2.31.jar jakarta.validation-api-2.0.2.jar swagger-integration-2.2.31.jar jakarta.ws.rs-api-2.1.6.jar swagger-jaxrs2-2.2.31.jar jakarta.xml.bind-api-2.3.3.jar swagger-jaxrs2-servlet-initializer-v2-2.2.31.jar javassist-3.30.2-GA.jar swagger-models-2.2.31.jar jersey-client-2.45.jar /opt/openfire/plugins/restapi/web: docs images WEB-INF /opt/openfire/plugins/restapi/web/docs: favicon-16x16.png swagger-ui.css swagger-ui.js favicon-32x32.png swagger-ui.css.map swagger-ui.js.map index.html swagger-ui-es-bundle-core.js swagger-ui-standalone-preset.js oauth2-redirect.html swagger-ui-es-bundle-core.js.map swagger-ui-standalone-preset.js.map swagger-ui-bundle.js swagger-ui-es-bundle.js swagger-ui-bundle.js.map swagger-ui-es-bundle.js.map /opt/openfire/plugins/restapi/web/images: error-16x16.gif success-16x16.gif /opt/openfire/plugins/restapi/web/WEB-INF: web.xml [root@yfw openfire]# ps aux | grep openfire root 133870 3.4 8.5 3704484 328488 ? Sl 16:23 0:11 /usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.el8_5.x86_64/bin/java -cp /opt/openfire/lib/*:/opt/openfire/lib/startup.jar org.jivesoftware.openfire.starter.ServerStarter root 136092 0.0 0.0 12136 1120 pts/0 R+ 16:29 0:00 grep --color=auto openfire [root@yfw openfire]# useradd -r -d /opt/openfire -s /sbin/nologin openfire useradd: user 'openfire' already exists [root@yfw openfire]# chown -R openfire:openfire /opt/openfire [root@yfw openfire]# kill 133870 [root@yfw openfire]# ps aux | grep openfire root 136952 0.0 0.0 12136 1196 pts/0 S+ 16:31 0:00 grep --color=auto openfire [root@yfw openfire]# su -s /bin/bash -c "/opt/openfire/bin/openfire.sh start" openfire OPENFIRE_HOME is empty, trying to find it OPENFIRE_HOME is set to /opt/openfire Option: start Error: Could not find or load main class start Caused by: java.lang.ClassNotFoundException: start [root@yfw openfire]# su - openfire Last login: Sun Nov 16 16:31:43 CST 2025 on pts/0 ✅ JAVA_HOME: /usr/lib/jvm/java-11-openjdk ✅ Maven home: /opt/maven ✅ Java version: openjdk version "11.0.13" 2021-10-19 LTS ✅ Environment loaded for openfire PATH = /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/lib/jvm/java-11-openjdk/bin:/opt/maven/bin:/usr/local/ffmpeg/bin:/opt/spark/bin JAVA_HOME = /usr/lib/jvm/java-11-openjdk [openfire@yfw ~]$ cd /opt/openfire [openfire@yfw openfire]$ ./bin/openfire.sh start Option: start Error: Could not find or load main class start Caused by: java.lang.ClassNotFoundException: start [openfire@yfw openfire]$ ps aux | grep openfire root 137241 0.0 0.1 146412 6304 pts/0 S 16:32 0:00 su - openfire openfire 137242 0.0 0.1 27276 4228 pts/0 S 16:32 0:00 -bash openfire 137459 0.0 0.1 58736 3964 pts/0 R+ 16:32 0:00 ps aux openfire 137460 0.0 0.0 12136 1168 pts/0 S+ 16:32 0:00 grep --color=auto openfire [openfire@yfw openfire]$ tail -f /opt/openfire/logs/info.log tail: cannot open '/opt/openfire/logs/info.log' for reading: No such file or directory tail: no files remaining [openfire@yfw openfire]$
最新发布
11-17
[root@hadoop1 export]# hdfs dfs -put D:\HadoopDemo\target\HadoopDemo-1.0-SNAPSHOT.jar /export/data -put: Can not create a Path from a null string Usage: hadoop fs [generic options] [-appendToFile <localsrc> ... <dst>] [-cat [-ignoreCrc] <src> ...] [-checksum [-v] <src> ...] [-chgrp [-R] GROUP PATH...] [-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...] [-chown [-R] [OWNER][:[GROUP]] PATH...] [-copyFromLocal [-f] [-p] [-l] [-d] [-t <thread count>] <localsrc> ... <dst>] [-copyToLocal [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>] [-count [-q] [-h] [-v] [-t [<storage type>]] [-u] [-x] [-e] <path> ...] [-cp [-f] [-p | -p[topax]] [-d] <src> ... <dst>] [-createSnapshot <snapshotDir> [<snapshotName>]] [-deleteSnapshot <snapshotDir> <snapshotName>] [-df [-h] [<path> ...]] [-du [-s] [-h] [-v] [-x] <path> ...] [-expunge [-immediate] [-fs <path>]] [-find <path> ... <expression> ...] [-get [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>] [-getfacl [-R] <path>] [-getfattr [-R] {-n name | -d} [-e en] <path>] [-getmerge [-nl] [-skip-empty-file] <src> <localdst>] [-head <file>] [-help [cmd ...]] [-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [-e] [<path> ...]] [-mkdir [-p] <path> ...] [-moveFromLocal <localsrc> ... <dst>] [-moveToLocal <src> <localdst>] [-mv <src> ... <dst>] [-put [-f] [-p] [-l] [-d] <localsrc> ... <dst>] [-renameSnapshot <snapshotDir> <oldName> <newName>] [-rm [-f] [-r|-R] [-skipTrash] [-safely] <src> ...] [-rmdir [--ignore-fail-on-non-empty] <dir> ...] [-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} <path>]|[--set <acl_spec> <path>]] [-setfattr {-n name [-v value] | -x name} <path>] [-setrep [-R] [-w] <rep> <path> ...] [-stat [format] <path> ...] [-tail [-f] [-s <sleep interval>] <file>] [-test -[defswrz] <path>] [-text [-ignoreCrc] <src> ...] [-touch [-a] [-m] [-t TIMESTAMP ] [-c] <path> ...] [-touchz <path> ...] [-truncate [-w] <length> <path> ...] [-usage [cmd ...]] Generic options supported are: -conf <configuration file> specify an application configuration file -D <property=value> define a value for a given property -fs <file:///|hdfs://namenode:port> specify default filesystem URL to use, overrides 'fs.defaultFS' property from configurations. -jt <local|resourcemanager:port> specify a ResourceManager -files <file1,...> specify a comma-separated list of files to be copied to the map reduce cluster -libjars <jar1,...> specify a comma-separated list of jar files to be included in the classpath -archives <archive1,...> specify a comma-separated list of archives to be unarchived on the compute machines The general command line syntax is: command [genericOptions] [commandOptions] Usage: hadoop fs [generic options] -put [-f] [-p] [-l] [-d] <localsrc> ... <dst>
05-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MineGi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值