如何在Linux中查看日志文件并在查看时应用自定义过滤器?

Dan*_*Dan 42 linux logfiles filter

我需要阅读Linux系统上的一些巨大的日志文件.日志中有很多混乱.目前我正在做这样的事情:

cat logfile.txt | grep -v "IgnoreThis\|IgnoreThat" | less
Run Code Online (Sandbox Code Playgroud)

但它很麻烦 - 每次我想添加另一个过滤器时,我都需要退出less并编辑命令行.一些滤波器相对复杂并且可以是多线的.

我想通过日志阅读一些方法来应用过滤器,以及在某处保存这些过滤器的方法.

有没有可以为我做这个的工具?我无法安装新软件,所以希望它已经安装好了 - 例如,less,vi,Python或Perl lib等等.

更改生成日志以生成更少的代码不是一种选择.

小智 105

&pattern在less内使用命令.

从手册页中减去

&图案

          Display  only  lines which match the pattern; lines which do not
          match the pattern are not displayed.  If pattern  is  empty  (if
          you  type  &  immediately  followed  by ENTER), any filtering is
          turned off, and all lines are displayed.  While filtering is  in
          effect,  an  ampersand  is  displayed  at  the  beginning of the
          prompt, as a reminder that some lines in the file may be hidden.

          Certain characters are special as in the / command:

          ^N or !
                 Display only lines which do NOT match the pattern.

          ^R     Don't interpret regular expression  metacharacters;  that
                 is, do a simple textual comparison.
Run Code Online (Sandbox Code Playgroud)

  • 当我发现这个问题时,这正是我所寻找的.而且我认为这正是所有者要求的问题.这应该是公认的答案!无论如何,非常感谢,你为我节省了很多时间! (2认同)

gar*_*les 4

尝试使用multitail工具 - 除了让您一次查看多个日志之外,我很确定它还可以让您交互地应用正则表达式过滤器。