[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Highlighting CLI output: what are these terms called?



Dotan Cohen wrote:
> The following page has a nice example of how to highlight text in logfiles:
> http://www.euperia.com/linux/how-to-highlight-keywords-when-using-tail/903
> 
> Here is the example:
> tail -f file.log | perl -pe 's/keyword/\e[1;31;43m$&\e[0m/g'
> 
> What are the regex replacements in the second part of the replace
> called? They are rather hard to goolge for without a name! If anybody
> has a good resource bookmarked with examples, I would love to see that
> as well.

The only regexp replacement in the right hand side is "$&".  The
perlre docs say:

   man perlre

       $& returns the entire matched string.  (At one point $0 did
       also, but now it returns the name of the program.)

This comes from use in 'sed'.  In sed the docs say:

   man sed

   s/regexp/replacement/
       Attempt to match regexp against the pattern space.  If
       successful, replace that portion matched with replacement.  The
       replacement may contain the special character & to refer to
       that portion of the pattern space which matched, and the
       special escapes \1 through \9 to refer to the corresponding
       matching sub-expressions in the regexp.

And can be used like this:

  $ echo fore | sed 's/fore/be&/'
  before

All of the \e [ (escape bracket) patterns are terminal escape sequence
colors as the others mentioned.  Personally I find using them like
that annoying since they are non-portable.  It would be better to use
'tput' to generate those from the terminfo data instead.

> tail -f file.log | perl -pe 's/keyword/\e[1;31;43m$&\e[0m/g'

Instead of that I would be inclined to use grep's --color option.
Same thing but easier to type and remember.

  tail -f file.log | grep --color keyword

Bob

Attachment: signature.asc
Description: Digital signature


Reply to: