如何使用sed在模式后插入任意字符串 [英] How to insert an arbitrary string after pattern with sed
问题描述
它一定很简单,但不知为何我不明白……我想通过 bash 脚本处理 HTML 文件并将 HTML 字符串插入某个节点:
org.html:<div id="wrapper"></div>
MYTEXT=$(phantomjs capture.js www.somesite.com)# MYTEXT 看起来像这样:# <div id="test" style="top: -1.9%;">Something</div>sed -i "s/\<div id=\"wrapper\"\>/\<div id=\"wrapper\"\>$MYTEXT/" org.html
我总是收到这个错误:bad flag in replacement command: 'd'
这可能是因为 sed
解释了 $MYTEXT
的内容也作为一种模式——这不是我想要的......
顺便说一句:复制 \<div id=\"wrapper\"\>
可能也没有必要?
$MYTEXT
的
好像是/
code> 部分确实被解释为 sed
命令中的最终 /
.您可以选择其他分隔符,该分隔符不会出现在 $MYTEXT
中,例如:
sed -i "s|\It must be really easy, but somehow I don't get it… I want to process an HTML-file via a bash script and insert an HTML-String into a certain node:
org.html: <div id="wrapper"></div>
MYTEXT=$(phantomjs capture.js www.somesite.com)
# MYTEXT will look something like this:
# <div id="test" style="top: -1.9%;">Something</div>
sed -i "s/\<div id=\"wrapper\"\>/\<div id=\"wrapper\"\>$MYTEXT/" org.html
I always get this error: bad flag in substitute command: 'd'
which is probably because sed
interprets the content of $MYTEXT
as a pattern as well – which is not what I want…
By the way: Duplicating \<div id=\"wrapper\"\>
is probably also not necessary?
解决方案 It seems the /
in $MYTEXT
's </div>
part is interpreted indeed as the final /
in the sed
command. You can choose another delimiter, which does not appear in $MYTEXT
, for instance:
sed -i "s|\<div id=\"wrapper\"\>|\<div id=\"wrapper\"\>$MYTEXT|" org.html
这篇关于如何使用sed在模式后插入任意字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文