0

I have a function setup to provide color to the tail command using ANSI escape sequences with sed.

function colortail {
tail -f -n30 $1 | sed -e 's/\(.*MINOR.*\)/\o033[93m\1\o033[39m/' -e 's/.*MAJOR.*/\o033[38;5;202m&\o033[m/' 
}

How can I set this up so that any other lines that do not match my predefined strings are in another color? Like a "default color"? Is there a way to wildcard this? Would adding something like this work if I wanted bright/bold white?

-e 's/\(.*"".*\)033[1;37m\033[m"
1

I'm not very good with shell escape sequences, but it seems to me that you are. I assume that what you're missing for your use-case, therefore, is to implement if/else-type logic. Here's one way you can do it:

sed -e '\
        /.*MINOR.*/{ # If .*MINOR.* is matched \
            # Code to set MINOR color \
        } \
        /.*MINOR.*/!{ # If .*MINOR.* is not matched \
            /.*MAJOR.*/ { # If .*MAJOR.* is matched \
            # Code to set MAJOR color \
            } \
            /.*MAJOR.*/!{ # If .*MAJOR.* is not matched \
            # Code to set the default color \
            } \
         }'

Reference

  • I hadn't thought about that but I think I can build off of it. I will give it a shot. – user53029 Jul 6 '16 at 21:02

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.