2

I am currently using the following command to use the standard ANSI colors with the tail command:

tail -f syslogfile | sed -e 's/\(.*MAJOR.*\)/\o033[93m\1\o033[39m/'

This is bright yellow however I am just getting into extended 256 colors in bash and I would like to use the color orange. I have found the correct foreground and background color numbering scheme to give me orange on black.

echo -e "\\033[40;5;95;38;5;202mhello world\\033[0m"

However I am having trouble translating this into a sed command that I can use with the tail command.

I have tried this:

sed -e 's/\(.*MAJOR.*\)/\033[40;5;95;38;5;202\033[0m/'

But that leaves everything white. I have tried:

sed -e 's/\(.*MAJOR.*\)/\o033[40;5;95;38;5;202\o033[0m/'

But this blacks out the lines that contain the string "MAJOR". I have also tried leaving out the background scheme and just going with the orange :

sed -e 's/\(.*MAJOR.*\)/\033[38;5;202\033[0m/'

But this also seems to black out the lines that contain the word "MAJOR" Does anyone know what the correct ANSI sequence would be for sed to give me orange on black? FYI the sed command I used in my first example prints the entire line in the color chosen and I would need the same behavior with the new color sequence, not just coloring the word itself.

UPDATE:

This works:

sed -e 's/\(.*MINOR.*\)/\o033[38;5;202m\1\o033[40;5;95m/'

But leaves other lines that do not contain MINOR colored magenta. How do I get those to default back to white. Am I not turning something off correctly? It's actually making default font color for the entire shell magenta, as I can see when I terminate the tail command.

  • there are several programs that already do colour highlighting of log files etc, and there are several questions on this site about them. e.g. see unix.stackexchange.com/questions/8414/… - and the Linked and Related questions. – cas Jun 22 '16 at 0:42
2

You are probably looking for this:

sed -e 's/\(.*MAJOR.*\)/\o033[48;5;95;38;5;202m\1\o033[0m/'

although it can be simplified with

sed -e 's/.*MAJOR.*/\o033[48;5;95;38;5;202m&\o033[m/'

Notice change of 40 to 48 what means we want to change background color.

If you want background color as black:

sed -e 's/.*MAJOR.*/\o033[40;38;5;202m&\o033[m/'

and if background color is already black you don't need to change it at all:

sed -e 's/.*MAJOR.*/\o033[38;5;202m&\o033[m/'
  • You had it right the first time - 40;5;95m. Using 48;5;95 gives a light orange transparent background. – user53029 Jun 21 '16 at 20:06
  • Put simply I just want lines with MAJOR colored orange with a black background. – user53029 Jun 21 '16 at 20:08
  • thanks for teaching me about & vs. \1. +1 – Alex Stragies Jun 21 '16 at 20:11
  • @user53029 Then I don't understand why did you put 40,5,95 code. It changes background color to black, then add blinking (rarely supported by terminals), then change foreground color to high intensity and then once again change foreground color - this doesn't make sense to me. Anyhow added some more examples to the answer. – jimmij Jun 21 '16 at 20:14
  • Yea, I'm new to all this 256 bash color stuff. – user53029 Jun 21 '16 at 20:17
3

There is more than one problem, seen by making a script of your examples:

#!/bin/sh
MSG="this is MAJOR stuff"
echo "$MSG" | sed -e 's/\(.*MAJOR.*\)/\o033[93m\1\o033[39m/'
echo "$MSG" | sed -e 's/\(.*MAJOR.*\)/\033[40;5;95;38;5;202\033[0m/'
echo "$MSG" | sed -e 's/\(.*MAJOR.*\)/\o033[40;5;95;38;5;202\o033[0m/'
echo "$MSG" | sed -e 's/\(.*MAJOR.*\)/\033[38;5;202\033[0m/'

which produces (^[ is escape):

^[[93mthis is MAJOR stuff^[[39m
this is MAJOR stuff33[40;5;95;38;5;202this is MAJOR stuff33[0m
^[[40;5;95;38;5;202^[[0m
this is MAJOR stuff33[38;5;202this is MAJOR stuff33[0m

In the first line, a ^[[93m is the aixterm 16-color escape to set a yellow text, and ^39m resets text to the terminal's default colors.

The second line has problems with an unterminated escape sequence (that starts off setting background to black=40, blink=5, 95=bright magenta text, then 38 and 5 try to introduce a 256-color sequence -- which is not terminated).

The third line is a slight variation of the second.

The main problem is that your sed does not emit an escape character unless you express it as \o033 (which you did do in a few places).

Second, you are not ending the escapes properly. That final m is part of the sequence. When the final character is missing, the entire sequence can be discarded. It's best to read the documentation: XTerm Control Sequences.

By the way, ANSI never documented anything past 8 colors, so it's less confusing to refer to the sequences by the original implementation (aixterm for 16-colors, xterm for 256-colors).

If you simply want entire lines with MAJOR to be highlighted in orange, then (based on comments), you could have done this:

sed -e 's/^\(.*MAJOR.*\)$/\o033[48;5;95m\1\o033[m/'

However, the 5 code is less widely supported than the 2 using R/G/B, something like this:

sed -e 's/^\(.*MAJOR.*\)$/\o033[48;2;128;128;0m\1\o033[m/'

(the 128's are just a first try - your tastes can vary).

Further reading:

CSI Pm m Character Attributes (SGR).

  • Nice writeup up with lots of info to research for OP and others, but no fixed sed -e '...' solution for OP. ;) +1 – Alex Stragies Jun 21 '16 at 20:16

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.