Score:0

sed with ANSI color escapes fail

in flag

I do not know why this does not work:

echo -e "$(echo "This is an uncolored text" | sed "s/{This is an uncolored text}/{This is an \033[0;34uncolored text\033[0m}/g")"

but this works well:

echo "$(echo "Hello World" | sed "s/Hello/Hi/g")"

May you explain to me?

Bodo avatar
pt flag
Please [edit] your question and explain what exactly you mean with "this does not work". What actual output do you get? What do you want to get?
Bruno Henrique Peixoto avatar
in flag
The following command: start_command echo "this is an uncolored text" | sed 's/This is an uncolored text/This is an \\033\[0;34muncolored text\\033\[0m/g' end_commend still does not work
Bruno Henrique Peixoto avatar
in flag
Could you paste the final result. I do not get it only by substituting ```[``` by ```\[```
Bruno Henrique Peixoto avatar
in flag
The original problem takes an uncolored string and substitutes it by it colored one. However, as you can see, the colored version lacks some backslashes.
Bruno Henrique Peixoto avatar
in flag
Problem solved!
Score:2
hr flag

There are a number of issues.

First, the braces {...} in your sed expression are not matched by anything in the input. In a sed Basic Regular Expression, braces are literal, while in an Extended Regular Expression they surround a quantifier of the form {n,m}. They are never used for grouping.

Second, your color sequence is malformed - the opening needs to be \033[0;34m rather than \033[0;34

Third, the backslash character \ is special in sed - in particular, a backslash followed by a decimal digit on the RHS of a substitution is a backreference to a capture group; at least in GNU sed, \0 refers to the whole captured LHS equivalent to the special replacement token & so for example

$ echo foo | sed 's/foo/\033bar/'
foo33bar

To pass a literal \ to the outer echo -e you'd need \\ inside the sed replacement string.

Finally, \ is also special to the shell, so inside "soft" double quotes needs an additional backslash. So either:

echo -e "$(echo "This is an uncolored text" | 
  sed "s/This is an uncolored text/This is an \\\033[0;34muncolored text\\\033[0m/")"

or (replacing the inner double quotes with 'strong' single quotes):

echo -e "$(echo "This is an uncolored text" | 
  sed 's/This is an uncolored text/This is an \\033[0;34muncolored text\\033[0m/')"

Note that you don't need the g modifier to make a single replacement per line.

See also What characters do I need to escape when using sed in a sh script?

Bruno Henrique Peixoto avatar
in flag
I worked my way out. Take a look at the answer below.
Score:0
in flag

My worked out solution:

#!/bin/bash

colored_text='this is a \\033[0;34mtext\\033[0m'
uncolored_text='this is a text'

printf %b\\n "$(sed "s/$uncolored_text/$colored_text/" <<< "$uncolored_text")"

Bruno Henrique Peixoto avatar
in flag
Thanks, bac0n! It smells good.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.