【问题标题】:Less - search in multiple files at once少 - 一次搜索多个文件
【发布时间】:2021-12-28 12:38:48
【问题描述】:

您好,我想知道是否有可能使用 less 一次搜索多个文件。我遇到了问题,我应该通过多个日志(instance1.log、instance2.log、instance3.log ...)并搜索一些测试。现在,我可以使用 less instance?.log 在 less 中打开多个文件,但我需要手动搜索文件并单独搜索每个文件,但我想一次搜索所有文件。有这样的选择吗?

【问题讨论】:

    标签: linux unix logging less


    【解决方案1】:

    来自less 手册页的/pattern 命令:

              Certain characters are special if entered at  the  beginning  of
              the  pattern;  they modify the type of search rather than become
              part of the pattern:
    

    ...

              ^E or *
                     Search multiple files.  That is, if  the  search  reaches
                     the  END of the current file without finding a match, the
                     search continues in the next file  in  the  command  line
                     list.
    

    所以比如用less来打开多个文件:

    less file1 file2
    

    然后输入/^E/*,然后输入您的搜索模式。使用n 向前导航模式匹配,使用shift+n 向后导航模式匹配(跨越多个文件)。

    【讨论】:

    • 非常感谢,这正是我想要的。
    【解决方案2】:

    这是一个题外话,但您可以使用grep。看看这个answer

    【讨论】:

      【解决方案3】:

      我不确定这是否是你想要的,但你可以做到

      cat instance?.log | less
      

      然后搜索结果。

      或者使用 grep,因为它基本上是为此用例制作的工具:

      grep -E 'search term' instance?.log
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-30
        • 2015-01-12
        • 1970-01-01
        • 2016-08-23
        • 1970-01-01
        • 2010-10-16
        相关资源
        最近更新 更多