首页 > 解决方案 > 来自 nl 和 tail 的行号之间的差异

问题描述

使用以下命令时:

nl /etc/snort/snort.conf | grep output

我得到以下输出:

 33  #  6) Configure output plugins
445  #  Step #6: Configure output plugins
450  #  output unified2: filename snort.log, limit 128, mpls_event_types, vlan_event_types

所以,我可以看到它Step #6: Configure output plugins在第 445 行。

我想输出第 445 行加上前五行(440-444 + 445),所以我使用:

tail -n+440 /etc/snort/snort.conf | head -n 6

但是,这给了我完全不同的结果。所以,我用行号对整个文件进行分类,调查并确实看到该行# Step #6: Configure output plugins位于第 445 行...

在对 tail 命令进行多次试验和错误之后,我终于得到了预期的结果,但是我最初认为在 445 上的行实际上是在 529 上。我可以通过将先前的命令编号更改为来验证这一点:

tail -n+524 /etc/snort/snort.conf | head -n 6

然后我得到了最初预期的结果,显示了五行配置文件,# Step #6: Configure output plugins作为输出的最后一行。

为什么感知的行号(445 与 529)之间存在差异?

标签: linuxtailline-numbers

解决方案


看看原始输出nl。它不给空行编号。

$ nl /etc/snort/snort.conf
...
    32  ###################################################
    33  # Step #1: Set the network variables.  For more information, see README.variables
    34  ###################################################

    35  # Setup the network addresses you are protecting
    36  ipvar HOME_NET any

    37  # Set up the external network addresses. Leave as "any" in most situations
    38  ipvar EXTERNAL_NET any

    39  # List of DNS servers on your network
    40  ipvar DNS_SERVERS $HOME_NET

    41  # List of SMTP servers on your network
    42  ipvar SMTP_SERVERS $HOME_NET

    43  # List of web servers on your network
    44  ipvar HTTP_SERVERS $HOME_NET
...

用于-ba对所有行进行编号。默认值为-bt:仅编号非空行。

nl -ba /etc/snort/snort.conf | grep output

推荐阅读