[Discuss] sed and ANSI sequences

David Kramer david at thekramers.net
Tue Oct 18 14:04:38 EDT 2011


On 10/16/2011 08:01 PM, Derek Martin wrote:
>>> echo "This is a [WARNING] message " | sed -e 's/.*[WARNING].*/'${COLOR_WARN}'&'${COLOR_RESET}'/g;'
> 
> This should work, once you properly escape the 'e':
> 
> msg=`echo "This is a [WARNING] message " | sed 's//.*[WARNING].*/'${COLOR_WARN}'&'${COLOR_RESET}'/g;'`
> echo -e $msg

What eventually worked very well for me, after reading all your posts,
was to use "echo -e" to set the color variables, then I don't need any
special treatment after that in sed.

eg
export COLOR_WARN=`echo -e '\e[33;40m'`

then I can just do

echo "This is a [WARNING] message " | sed -e
's/.*[WARNING].*/'${COLOR_WARN}'&'${COLOR_RESET}'/g;'

Thanks for all your help.  The only real problem I'm having at this
point is matching square brackets.  Since square brackets need to be
escaped even when maching a set of characters, I don't know how to tell
it to match an open square bracket then characters in a set, then a
close square bracket.

This is what I mean: Lets say I want to match [ followed by lower case
letters or a period, followed by ].
MATCH:  [net.thekramers.foo]
NOT A MATCH:  net.thekramers.foo
NOT A MATCH:  [net.thekramers.Foo!]




More information about the Discuss mailing list