[Discuss] sed and ANSI sequences

Nuno Sucena Almeida slug at aeminium.org
Thu Oct 13 17:25:31 EDT 2011


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.



More information about the Discuss mailing list