[Discuss] sed and ANSI sequences

David Kramer david at thekramers.net
Fri Oct 14 00:37:24 EDT 2011


On 10/13/2011 05:25 PM, Nuno Sucena Almeida wrote:
> On Thu, Oct 13, 2011 at 03:00:04PM -0400, David Kramer wrote:
> | PS: I did see one working example at
> | http://travelingfrontiers.wordpress.com/2010/08/22/how-to-add-colors-to-linux-command-line-output/
> | sed ''/crit/s//$(printf "\033[31mCRITICAL\033[0m")/g''
> | 
> | However, it relies on the printf command, which means (1)launching another process and 2) not
> | using sed's buffers, so I can't have a series of sed operations in one command.
> 
> I use something similar to this, also running an external command (echo). Here's an example, using the -e
> sed command argument
> 
> function getinfo()
> {
>     DEV="$1"
>     WHAT="$2"
>     REGEXP="s,.*${WHAT}:\([0-9.]\+\).*,\1,p"
>     ifconfig ${DEV} | sed -n -e "$(echo ${REGEXP})"
> }
> 
> AFAIR I went through the same issues and apparently you cannot directly pass
> variables to sed. If you feel thank you can do the same with (g)awk
> instead, you can pass variables to it.

I had no problems passing parameters to sed.  I solved that problem by
putting the environment variables outside the quotes and letting the
interpreter concatenate the strings:

sed -e
's/.*[WARNING].*/\'${COLOR_WARN}'&\'${COLOR_RESET}'/g;'

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.



More information about the Discuss mailing list