Usable technology

Peter Kuhar

Using sed to color specific words in Unix consoles

I monitoring live logs bringing out some words is very helpfull. Implementing this in unix console is straightforward.

Let’s you monitor a log with tail

tail -f log.txt

You can color matched words with ANSI Console escape sequences.

tail -f log.txt | sed “s/was/”$(echo -e “\\033[32m”)”WAS”$(echo -e “\\033[0m”)”/g”

This for example translates “pero was here” into “pero WAS here”. Notice the green color.

How it works is that sed replaces every was with WAS + some escape sequences to change color to green and back. -e in echo command makes the \ sequences to work.

You can use this in almost any console.

Leave a Reply

You must be logged in to post a comment.

http://www.pkuhar.com © 2006 Peter Kuhar peter@pkuhar.com