48

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

cat logfile.txt | grep -v "IgnoreThis\|IgnoreThat" | less

但这很麻烦——每次我想添加另一个过滤器时,我都需要退出less并编辑命令行。有些过滤器比较复杂,可能是多行的。

我想要在阅读日志时应用过滤器的方法,以及将这些过滤器保存在某处的方法。

有没有工具可以为我做到这一点?我无法安装新软件,所以希望它是已经安装的东西——例如,less、vi、Python 或 Perl lib 中的东西等。

将生成日志的代码更改为生成更少的代码不是一种选择。

4

5 回答 5

113

在 less 内使用&pattern命令。

从手册页中获得更少

&图案

          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.
于 2012-07-04T12:36:41.833 回答
4

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

于 2010-02-26T01:31:30.357 回答
4

根据ghostdog74的回答和手册less页,我想出了这个:

~/.bashrc

export LESSOPEN='|~/less-filter.sh %s'
export LESS=-R  # to allow ANSI colors

~/less-filter.sh

#!/bin/sh
case "$1" in
*logfile*.log*) ~/less-filter.sed < $1
  ;;
esac

~/less-filter.sed

/deleteLinesLikeThis/d  # to filter out lines
s/this/that/  # to change text on lines (useful to colorize using ANSI escapes)

然后:

  • less logfileFooBar.log.1-- apply 过滤器自动应用。
  • cat logfileFooBar.log.1 | less-- 不过滤查看日志

现在这已经足够了,但我仍然希望能够即时编辑过滤器。

于 2010-02-26T02:14:27.743 回答
0

请参阅 less 的手册页。例如,您可以使用一些选项来搜索单词。它也有行编辑模式。

于 2010-02-26T01:04:48.273 回答
0

Casstor Software Solutions 有一个名为 LogFilter (www.casstor.com) 的应用程序,它可以编辑 Windows/Mac/Linux 文本文件,并且可以轻松执行文件过滤。它支持多个过滤器以及正则表达式。我想这可能是你正在寻找的。

于 2015-12-08T22:58:48.730 回答