Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Server
User Name
Linux - Server This forum is for the discussion of Linux Software used in a server related context.


  Search this Thread
Old 05-09-2012, 03:49 AM   #1
Registered: Aug 2009
Posts: 30

Rep: Reputation: 0
How to convert specific word into specific colors in console shell?


Just come up to my mind, I'm using openSuSE 12.1 for my mail server. In openSuSE 12.1 every grep word will come up with different colors (red).

Is there any way when I tail -f /var/log/mail every 'status=sent' word is in red color without grep only 'status=sent'? so the whole mail log are displayed but every 'status=sent' word come up with red color.

Old 05-09-2012, 04:54 AM   #2
Registered: Mar 2011
Location: Rome - Italy
Distribution: Slackware 13.1
Posts: 347

Rep: Reputation: 44
tail -f /var/log/mail | egrep --color -i 'status=sent|'
Old 05-09-2012, 11:18 AM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Terminal colors are produced through the use of ansi escape codes, hidden markup characters inserted into the text that control how the terminal displays things.

grep, with the --color option, inserts color codes into the output text stream whenever it finds a match. tail, on the other hand, does not have that ability, so your only choice is to subsequently filter it through a program or script that does.

Note also that most programs that produce color output only do when the output is being sent directly to a terminal. When the output is being piped to a second program, or redirected into a file, colors are usually disabled, as the extra characters in the text are probably unwanted and can mess with subsequent operations.

But in grep, ls, and many other programs that produce color output, you can use --color=always, --color=auto, or --color=never to control when the codes are inserted (auto the default behavior).

To see an example of the codes, try running:
echo "foo bar baz" | grep --color=always 'bar' >textfile
...and then view the file in a standard text editor. Assuming grep matched something You should see the markup characters embedded in the line, like this:

foo bar baz
But if you cat the file back into the terminal, you'll see it turn red again.
Old 05-10-2012, 12:40 AM   #4
Registered: Aug 2009
Posts: 30

Original Poster
Rep: Reputation: 0
Thanks very much all!! It works now


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read lines after a specific word. Kashif_Bash Programming 10 04-26-2012 04:06 AM
Replace a specific word every three occurences udiubu Programming 5 12-07-2011 10:58 AM
printing a specific word out of a file. rossk Programming 9 01-23-2011 06:11 PM
SED ? get number before specific word czezz Programming 3 08-11-2009 06:00 PM
How to make a specific command(s) work in a specific shell only naren_0101bits Linux - Newbie 3 08-28-2005 01:24 PM > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:04 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration