[Discuss] sed and ANSI sequences

Daniel Hagerty hag at linnaean.org
Fri Oct 14 11:00:24 EDT 2011


David Kramer <david at thekramers.net> writes:

> You see, the color variables are actually not inside the quotes.  That
> part works fine.
> 
> The problem I'm having is the ANSI color escape codes are being printed
> instead of interpreted.

    Your problem is that you aren't asking sed to print an escape,
you're asking for either 'e' or '\e', depending on which particular
example.

    This works, for example:

esc="`printf '\033'`"
random_escape="${esc}bar"
sed -e 's/foo/'$random_escape'/'

$ echo foobaz | sh /tmp/esctest.sh |cat -v
^[barbaz

    The important part is you need to generate an actual escape
somehow.  Further, this assumes that sed will pass that escape through
unmolsted, which works for me, but may not do so for you.

    Here, I used printf, but you might be able to use echo, depending
on your implementation.  tr might work.

    If there's a way in sed to directly specify arbitrary escapes, I
don't know what it is.



More information about the Discuss mailing list