Colorize Tail Output

Colorize tail output

yes, there is way to do this. That is, as long as your terminal supports ANSI escape sequences. This is most terminals that exist.

I think I don't need explain how to grep, sed etc. point is the color right?

see below, this will make

WARN yellow
ERROR red
foo green

here is example:

kent$ echo "WARN
ERROR
foo"|sed 's#WARN#\x1b[33m&#; s#ERROR#\x1b[31m&#; s#foo#\x1b[32m&#'

Note: \x1b is hexadecimal for the ESC character (^VEsc).

to see the result:

Sample Image

Bash script to tail -f with colored lines

Do not quote the argument variable:

tail -f input | perl -pe 's/.*'$1'.*/\e[1;31m$&\e[0m/g'

You can also use grep for this:

tail -f input | grep -e $1 -e ''  --color=always

and to color the whole line with grep:

tail -f input | grep -e ".*$1.*" -e ''  --color=always

UNIX colored find output

As far as I know find doesn't have this built in. For situations like these I like to use grc (check out https://manpages.ubuntu.com/manpages/jammy/en/man1/grc.1.html). Hope that's helpful!

How do you automatically colorize program outputs in a bash shell?

#!/bin/sh
redf=$(tput setaf 1)
redb=$(tput setab 1)
reset=$(tput op)
echo "${redf}red${reset} in front, ${redb}red${reset} in back"

See terminfo for a long listing of terminal capabilities. A $TERM with suffix -m (e.g. ansi-m) means the screen is monochrome, but as long as color works, the following string capabilities should be non-empty:


enter_bold_mode bold md turn on bold (extra
bright) mode
enter_italics_mode sitm ZH Enter italic mode
enter_reverse_mode rev mr turn on reverse
video mode
orig_pair op op Set default pair to
its original value
set_a_background setab AB Set background
color to #1, using
ANSI escape
set_a_foreground setaf AF Set foreground
color to #1, using
ANSI escape

Colors 0-7 are pretty much standard: black, red, green, yellow, blue, magenta, cyan, white. Beyond that may not exist or may be more variable.

Bash: Syntax highlighting for tail (with awk)

I use next for else-ish behavior in awk.

/INFO/ {print 1; next}
/ERROR/ {print 2; next}
{print 3}

The next statement forces awk to immediately stop processing the current record and go on to the next record. This means that no further rules are executed for the current record, and the rest of the current rule’s action isn’t executed.

Thus, this can be read as:

If record matches /INPUT/ then print 1
else if record matches /ERROR/ then print 2
else print 3

tail & grep file and uniquely color by IP addresses

Here's a possibility in perl: tail -f YOURFILE| perl -pe 's/\d{1,3}\.\d{1,3}.(\d{1,3})\.\d{1,3}/\033[38;5;\1\2\3m$&\033[39m/g'

It replaces each IP-address looking substring with an ANSI color sequence based on its third octet.

It works the same in re2g: tail -f YOURFILE| re2g -gp '\d{1,3}\.\d{1,3}.(\d{1,3})\.\d{1,3}' -s $'\033[38;5;\\1\\2\\3m\\0\033[39m'

In perl, you can get a little fancier with your color choices though: tail -f YOURFILE| perl -pe 's/(\d{1,3})\.(\d{1,3}).(\d{1,3})\.(\d{1,3})/"\033[38;5;".(16+($1+$2+$3+$4)%214)."m$&\033[39m"/ge'. This version guarantees that the color falls into nice visible range and also bases the color on all four octets.

See also: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

How to preserve colors in logs with Multitail?

It's all about interpreting ANSI escape sequences which does terminal not tail itself and have to do multitail as well.
It can be done with -cT ANSI option:

-cT term    interpret terminal-codes from file/command (for terminal type 'term')

Example:

$ multitail -cT ansi log/development.log
multiple files:
$ multitail -cT ansi log/development.log -cT ANSI log/test.log

colorized output of multitail -cT ansi log/development.log



Related Topics



Leave a reply



Submit