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

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

Dotan Cohen wrote:
> Bob Proulx wrote:
> >> 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
> >
> Thank you Bob. I like how you get to the source (sed) and to the
> problem (use grep). Very informative, and I'll be reading this post a
> few times over.

I don't know if sed is the canonical source.  Regular expressions have
been around for a long time.  There were variations.  Perl's use of
them definitely pulls in from a variety of places and sed's
substitutions is definitely one of them.

But I made a mistake in the above.  Using grep is not quite the same
as the perl above it.  Sorry.  It might be better however because it
does remove the non-matching lines.

Perl's -p does:

       -p   causes Perl to assume the following loop around your program,
            which makes it iterate over filename arguments somewhat like sed:

                while (<>) {
                    ...             # your program goes here
                } continue {
                    print or die "-p destination: $!\n";

And so the perl in the above will highlight the keyword the same, yes,
but the perl will pass non-matching lines through too.  The perl is
performing the substitution on the default which is assigned to by the
default while (<>) and used by s///.  But regardless of the
substitution it is printed in the continue part of the while.  My grep
suggestion will not print the non-matching lines.  It is doing
something different.

That might be a good feature if you are really only interested in
lines with those keywords.  It will then focus only on those items.
But my suggestion of grep was a mistake in that it isn't the same as
the perl.  Let me make up for that mistake by suggesting another
alternative that you might also find useful in the toolbox.

I often use "less" to look at files.  Less will highlight searched
items.  Therefore as (yet another) simpler way to do highlighting let
me describe using less for it.

  less -i /var/log/syslog

Searching for whatever you want to search for will also highlight it.
Therefore the /PATTERN will search for and highlight that pattern.
Then F will follow the file just like 'tail -f'.  But since the search
highlighting is still active any new lines that appear in the file
will continue to be highlighted.  Do something that appends to the
file you are following and you will see it appear and the pattern will
be highlighted.  I find this very useful.

The -i is because I usually ignore case in less searches for things
like patterns in logfiles.  I set that option in a LESS=-i variable in
my .bashrc file so that I always get it and don't need to specify it.
Or you can type in "-i" to less and it will toggle case matching.  So
you can change it on the fly as you desire.


Attachment: signature.asc
Description: Digital signature

Reply to: