4

I'm trying to view large log files. I have a complex shell script that will take a log file and run it through a tangle of text-mangling commands to insert formatting and colouring, and pipe the result to less.

That all works great! Until I want to search for something... Now I frequently find that less fails to find my search term because of the colour escapes in the way.

Is there some way I can make less ignore colour escapes?

I don't want to remove them (I just expended a hell of a lot of efforts purposely putting them in there!). I just want them to be ignored for searching purposes. I still want the text to display in colour.

Any suggestions?

2 Answers 2

1

try -R or -r switch - you'll be able to see the colors properly (or just imagine them), as it will interpret the escapes "properly".

From less man:

   -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.
-1

As a workaround, you could put .* in between words, which would skip over everything in between, including the control characters.

So instead of searching for lorem ipsum dolor rosat you would search lorem.*ipsum.*dolor.*rosat.

Disclaimer: Note that it might match some unwanted sequences, but that should not be a big problem for multiple search words since matches are only found in a single line.

2
  • that's one hell of a nasty workaround ;)
    – Bart
    Jul 26, 2019 at 11:28
  • 2
    @Bart I'll take that as a compliment ;-)
    – mat
    Jul 26, 2019 at 11:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .