bash 在 sed 表达式中转义替换字符串中的“\”字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1064352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:02:06  来源:igfitidea点击:

Escaping the '\' character in the replacement string in a sed expression

bashstringsedescaping

提问by Chris Lieb

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)

并将其更改为(我希望 bash 颜色代码为输出着色,而不是显示在屏幕上)

3[32m*3[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.

使用 bash 颜色代码为星号着色,使其更加突出。我有一个 sed 命令可以完成大部分工作,只是它不能正确处理颜色代码,因为它将它们视为对替换文本的引用。

What I have so far:

到目前为止我所拥有的:

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

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.

它采用 \033 的 \0 并将其替换为原始字符串。将替换字符串中的反斜杠加倍并没有什么区别;我仍然得到相同的输出文本。

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

如何将 bash 颜色转义插入 sed 替换表达式?

回答by Paused until further notice.

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

机会是sed你使用的是不知道的八进制,但可以理解十六进制。试试这个版本,看看它是否适合你(使用\x1b而不是\033):

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

回答by Hasturkun

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

您的 '\033' 实际上是单个ESC(转义)字符,要输出它,您可以使用以下任何一种:

  • \o033
  • \d027
  • \x1B
  • \c[for CTRL-[
  • \o033
  • \d027
  • \x1B
  • \c[为了 CTRL-[

回答by DreadPirateShawn

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

将替换字符串中的反斜杠加倍,并在 sed 表达式周围使用单引号而不是双引号:

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

This prevents the shell from interfering with the sed behaviour.

这可以防止外壳干扰 sed 行为。

~~~~~~~

~~~~~~~

Update:

更新:

Use a script to achieve color cleanly:

使用脚本来干净地实现颜色:

colorize.sh

着色.sh

#!/bin/sh

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

sed "s/ *13) \(.*\)/ $HIGHLIGHT*$NORMAL /" yourinputtext

回答by Mitsuhashi

I had the same problem and I solved in this way.

我有同样的问题,我以这种方式解决了。

You can try to change your code:

您可以尝试更改代码:

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

into this:

进入这个:

sed "s/ *13) \(.*\)/ $(echo '3[32m')*$(echo '3[0m') /"

The basic idea is to use 'echo' to print the escape command. It worked for me,

基本思想是使用 'echo' 打印转义命令。它对我有用,

回答by Mark

I use gawk

我用gawk

gawk -v string1=$STRING1 -v IGNORECASE=1 ' { gsub ( string1 , "3[1m&3[0m" ) ; print } '

回答by blispr

Try this :

尝试这个 :

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

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

即斜线-斜线-斜线-斜线-033