使用这些测试文件
$ cat snippet.js
hello/(world)
$ cat template.hbs
foo
${SNIPPET}
bar
我可以(某种程度上)复制您的错误(我使用了GNU sed 4.2.2):
$ sed "s/\${SNIPPET}/$(cat snippet.js)/" template.hbs
sed: -e expression #1, char 20: unknown option to `s'
您可以这样做,它将转义斜杠(斜杠是
s///
命令)
sed "s/\${SNIPPET}/$(sed 's,/,\\/,g' snippet.js)/" template.hbs
foo
hello/(world)
bar
或者,如果代码段占位符像我的占位符一样位于自己的行上,则可以使用其他sed命令:
sed '/\${SNIPPET}/{
# read the file into the stream
r snippet.js
# delete SNIPPET line
d
}' template.hbs
foo公司
你好/(世界)
酒吧
还有另一种方法
j=$(<snippet.js) # read the file: `$(<...)` is a bash builtin for `$(cat ...)`
sed "s/\${SNIPPET}/${j//\//\\\/}/" template.hbs