不要把变量的内容在SED为特殊字符 [英] Don't treat variable content as special characters in sed
本文介绍了不要把变量的内容在SED为特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有如下的 SED
命令:
sed -i "4i\ $CHANGES" ./CHANGELOG.rst
不过,我的 $更改
变量看起来是这样的:
title
-----
* list
* elem
* elem
因此,上面的命令失败,出现以下错误:的sed:-e前$ P $#pssion 1,CHAR 149:未知命令:'*'
。我明白,这是因为在 $更改
变量的内容偏于间preTED,但我怎么指定只使用字符串中的变量作为原料字符串,不跨preting它的内容?
Hence, the above command fails with the following error: sed: -e expression #1, char 149: unknown command: '*'
. I understand that this happens because the contents of the $CHANGES
variable are somewhat interpreted, but how do I specify to only use the string in the variable as a raw string, without interpreting its contents?
推荐答案
尝试
sed -i "4 i\
${CHANGES//$'\n'/\\$'\n'}" ./CHANGELOG.rst
由于@NeronLeVelu指出,
As @NeronLeVelu notes,
- 您需要在
换行我\\
- 在文本换行传递给
SED
的I
函数必须是\\
转义 - 这是bash的参数(变量)替换上述声明
- you need a newline after
i\
- newlines in text passed to
sed
'si
function must be\
-escaped - this is what the bash parameter (variable) substitution above does.
这篇关于不要把变量的内容在SED为特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文