views:

1631

answers:

4

I am trying to take a line of text like

    13) Check for orphaned Path entries

and change it to (I want the bash color codes to colorize the output, not display on the screen)

\033[32m*\033[0m Check for orphaned Path entries

with bash color codes to colorize the asterisk to make it stand out more. I have a sed command that does most of that, except it doesn't handle the color codes correctly since it sees them as references to replacement text.

What I have so far:

sed "s/ *13) \(.*\)/ \033[32m*\033[0m \1/"

which produces the following output when run on the string I gave at the beginning:

   13) Check for orphaned Path entries33[32m*  13) Check for orphaned Path entries33[0m Check for orphaned Path entries

It is taking the \0 of the \033 and replacing it with the original string. Doubling the backslashes in the replacement string doesn't make a difference; I still get the same output text.

How do I insert bash color escapes into a sed replacement expression?

+3  A: 

Double the backslashes in the replacement string, AND use single instead of double quotes around the sed expression:

sed 's/ *13) \(.*\)/ \\033[32m*\\033[0m \1/'

This prevents the shell from interfering with the sed behaviour.

~~~~~~~

Update:

Use a script to achieve color cleanly:

colorize.sh

#!/bin/sh

HIGHLIGHT=`echo -e '\033[32m'`
NORMAL=`echo -e '\033[0m'`

sed "s/ *13) \(.*\)/ $HIGHLIGHT*$NORMAL \1/" yourinputtext
DreadPirateShawn
While sed isn't trying to replace the \0 now, I'm still not getting colored output.
Chris Lieb
To be more specific, I get \033[32m*\033[0m output directly to the console, without being turned into color codes
Chris Lieb
Not quite sure what you mean... My aim was to solve the problem of escaping the '\' character in the replacement string, and did so -- I get the same output text you cite in your "change it to" string above. Beyond that, you know what you're looking for color-wise more than I do; just wanted to make sure you had the tools nailed down to do so. :-)
DreadPirateShawn
Just saw your update -- I misunderstood your "change it to" string, then... thought that's what you wanted to output. Hmm...
DreadPirateShawn
PS - My script works, but Dennis Williamson's answer accomplishes the same thing, and is far shorter. Use his.
DreadPirateShawn
Sorry for dulpicating your answer. I missed the back-ticks (and the echoes). That's one of the reasons I always use $() - improved readability since there's no confusion with single quotes.
Dennis Williamson
A: 

Try this :

sed  "s/ *13) \(.*\)/ \\\\033 \1/"

i.e. slash-slash-slash-slash-033

blispr
+3  A: 

Chances are that the sed you are using doesn't understand octal, but it may understand hex. Try this version to see if it works for you (using \x1b instead of \033):

sed "s/ *13) \(.*\)/ \x1b[32m*\x1b[0m \1/"
Dennis Williamson
if it does grok octal, use \o033
Hasturkun
@Hasturkun: That's why it pays to read the info file. Sometimes man just doesn't cut it. Thanks for the info.
Dennis Williamson
@Dennis: Do you mean that sometimes man does not have the same manual as info?
Masi
@Masi: It says as much in the sed man page
Hasturkun
+4  A: 

your '\033' is in fact a single ESC (escape) character, to output this you may use any one of the following:

  • \o033
  • \d027
  • \x1B
Hasturkun
+1 for the octal prefix - most programs just recognize the leading zero.
Dennis Williamson