less:搜索,但忽略特殊字符

less:搜索,但忽略特殊字符

我正在尝试查看大型日志文件。我有一个复杂的 shell 脚本,它将获取一个日志文件并通过一系列文本编辑命令运行它以插入格式和着色,并将结果通过管道传输到less.

这一切都很棒!直到我想搜索某些东西......现在我经常发现less由于颜色逃逸而无法找到我的搜索词。

有什么办法我可以做less 忽略颜色逃逸?

我不想消除它们(我只是花了很多精力才故意把它们放在那里!)。我只是希望出于搜索目的而忽略它们。我仍然希望文本以彩色显示。

有什么建议么?

答案1

尝试-R-r切换 - 您将能够正确地看到颜色(或者只是想象它们),因为它将“正确地”解释转义。

less 男人:

   -r or --raw-control-chars
          Causes "raw" control characters to be displayed.  The default is to display control characters using the caret notation; for  example,
          a control-A (octal 001) is displayed as "^A".  Warning: when the -r option is used, less cannot keep track of the actual appearance of
          the screen (since this depends on how the screen responds to each type of control character).   Thus,  various  display  problems  may
          result, such as long lines being split in the wrong place.


   -R or --RAW-CONTROL-CHARS
          Like -r, but only ANSI "color" escape sequences are output in "raw" form.  Unlike -r, the screen appearance is maintained correctly in
          most cases.  ANSI "color" escape sequences are sequences of the form:

               ESC [ ... m

          where the "..." is zero or more color specification characters For the purpose of keeping  track  of  screen  appearance,  ANSI  color
          escape sequences are assumed to not move the cursor.  You can make less think that characters other than "m" can end ANSI color escape
          sequences by setting the environment variable LESSANSIENDCHARS to the list of characters which can end a color escape  sequence.   And
          you  can  make less think that characters other than the standard ones may appear between the ESC and the m by setting the environment
          variable LESSANSIMIDCHARS to the list of characters which can appear.

答案2

作为解决方法,您可以.*在单词之间插入,这将跳过之间的所有内容,包括控制字符。

因此,您不应该搜索,而lorem ipsum dolor rosat应该搜索lorem.*ipsum.*dolor.*rosat

免责声明:请注意,它可能会匹配一些不需要的序列,但这对于多个搜索词来说不应该是一个大问题,因为匹配仅在单行中找到。

相关内容