2

I have made a program where I can read several logs from different programs, I want to colourize certain terms in the output , so I can find the more interesting parts. (I will not use grep or anything similar because I still need to read the entire log, I just want to make certain interesting terms different colours to find them quickly).

I found this, but again, it's not what I'm looking for. I am looking for a way to pipe the text.

2

Do use grep. Have ^ be one of your search terms so that you still get every line (but it will be uncolored, unless it contains visible matches).

Example:

grep -e '[[:upper:]][[:alnum:]]*' -e ^ <<EOF
This Is An
example file.
All lines 
in it will be printed, and all
words that start 
with a Capital
letter will be Colorized.
EOF

(This uses a heredoc (search the shell manual for "Here Documents"). Piped stdin or a real file would have been just as fine).

2
  • Could you give me an example? Sep 9 '15 at 10:19
  • Done __________
    – PSkocik
    Sep 9 '15 at 10:36
1

For colourizing the output of command or contents of a file, I can think of two easy methods that may work well:

  • grep - it can be made to show the rest of the file as well, and do multiple matches with a few advanced options - for example:

    grep --color -iE 'log|kernel' -C 999
    grep --color -iE 'log|kernel|$'
    

    The first searches for log and kernel case-insensitively, and shows the surrounding 999 lines. The second searches for log, kernel and a character present in every line (as suggested here) case-insensitively. For more info you may be able to consult man grep if it available, or read the manual for GNU grep or OSX grep.

  • python-pygments - should be available via pip. Can be used for syntax highlighting, it may have a lexer available for what you want to process.

Also, for generally formatting output, you may be able to use ANSI escape codes - e.g:

echo -e "\033[31mred\033[0m"
echo -e "\033[34mblue\033[0m"

The \033 is a escape character, and after that you can specify codes to do different things - you can specify multiple ones as well:

echo -e "\033[1;31mbold red\033[0m"
echo -e "\033[4;34mblue underlined\033[0m"

I'm not sure about support on OSX though... you may need to try a different escape character - colors in the terminal may also need to be enabled.

2
  • echo -e "\033[31mred\033[0m" Did work on my OS X. Sep 9 '15 at 13:31
  • Also, the first line with ´-C 999´ works good, but I still want to see the entire log even if there is no match, it only shows the entire log if there is a match. EDIT: The way I solved that was to include a word that is always there. Sep 9 '15 at 13:43
0

If you just want to arbitrarily change text colors, you can use ANSI escape sequences to do that. For example, ESC[31m will change the text color to red, ESC[37m will produce white, etc.

You could use sed to insert the escape codes for certain colors in specific places in the stream. Here's an example of someone doing this very thing.

In addition to changing colors, you can do lots of other screen output manipulations. Here's a reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.