8

I want to take any program that outputs to the screen, catch the output, and colorize certain keywords before they are output to the screen. For example, here's the normal program output:

bash# <program>
blah blah blah       <-- this output has no color

vs.

bash# <program>
blah blah blah       <-- this output is colorful

Ideally it doesn't matter what the program is. I'm just looking for a good way to incorporate more color into my konsoles.

Edit: Sorry, should've been clear. I'm not trying to just colorize shell script outputs.

1
  • Give Pete's suggestion a try: yourprogram|ack --passthru --color "(keyword1|keyword2|keyword3)" Commented Jul 7, 2009 at 10:03

5 Answers 5

7
#!/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.

Sign up to request clarification or add additional context in comments.

Comments

5

The ack program is a version of grep that does color highlighting of regular expression matches in its output. You could use it to do the coloring for you, or you could study its Perl code.

Another option would be to pipe to GNU's grep, with a --color=always or --color=auto argument.

Comments

3

You can write a colorizing script. There's an excellent guide here http://www.faqs.org/docs/abs/HTML/colorizing.html

3 Comments

Ugh... really shouldn't tell people to use ANSI or VT10x escapes manually. termcap and terminfo have existed for eons for the purpose of holding all sorts of terminal-specific information like this.
@ephemient, except nobody use a real terminal since decades, and it's now just a software standard: It's OK to assume a terminal emulator is VT10x compatible, to avoid ncurses issues.
By the way, some good references: ftp.cs.utk.edu/pub/shuford/terminal
2

You might want to look at something like colorex, or the suggestions at a similar question on unix.SE.

Comments

1

Try the simple and brilliant generic colouriser (homepage):

Generic Colouriser is yet another colouriser (written in python) for beautifying your logfiles or output of commands.

It's available in Debian and is preconfigured for many tools:

$ grc traceroute www.linux.org

enter image description here

$ grc tail -25 /var/log/syslog

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.