I was wondering if there is a program in the common unix toolset such as grep that instead of filtering the lines that contain a string, simply outputs the same input but highlighting or coloring the selected string.
I was thinking in doing it by myself (should be simple enough), but maybe it already exists as a unix command.
I’m planning in using it to monitor logs, so I would do something like this:
tail -f logfile.log | highlight "error"
Usually when I’m monitoring logs I need to find a particular string but I also need to know what is written before and after the string, so filtering sometimes is not enough.
Does something like that exist?
Thanks
6
I would use a simple sed
substitute command with ANSI escape codes put before and after the matching RE.
tail -f logfile.log | sed 's/(error)/^[[42m1^[[0m/g'
where ^[
is an ESC character that you can obtain by pressing ctrl-V ESC
If you do need a highlight
command, you can define it as a shell function:
highlight() { sed 's/'"$1"')/^[[42m1^[[0m/g' ; }