在转义替换字符串中“\\”字符在SED前pression [英] Escaping the '\' character in the replacement string in a sed expression

查看:150
本文介绍了在转义替换字符串中“\\”字符在SED前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图采取类似

    13) Check for orphaned Path entries

并将其更改为(我想在bash颜色codeS上色输出,屏幕上不显示)

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

在bash颜色codeS上色的星号,使之更加突出。我有一个sed命令,做的大部分,除了它不能处理的颜色codeS正确的,因为它把它们看作以替换文本引用。

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.

我到目前为止有:

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

它走的是\\ 033的\\ 0和原始字符串替换它。在替换字符串倍增反斜线不会有所作为;我仍然得到同样的输出文本。

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.

我要如何插入庆典颜色逃进一个sed替换前pression?

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

推荐答案

机会是 SED 您使用的是不理解八进制,但它可能会理解十六进制。试试这个版本,看它是否为你的作品(使用 \\ X1B 而不是 \\ 033

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/"

这篇关于在转义替换字符串中“\\”字符在SED前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆