使用 sed 用多行替换一个单词? [英] Replace a word with multiple lines using sed?
问题描述
我正在编写一个 bash 脚本,它必须准备发送给用户的电子邮件.
I'm working on a bash-script that has to prepare an E-Mail for being sent to a user.
它聚合了一些数据,最终变成了多行内容.对于存储在 $DATA
中的示例.
It aggregates some data, which ends up being multiple lines of stuff. For the example stored in $DATA
.
现在,经过一些 stfw 我发现了一些东西,比如 sed -ei "s/_data_/${DATA}/g" mail.tpl
和 sed 用多行变量替换.它们都不起作用.
Now, after a bit of stfw I found a few things like sed -ei "s/_data_/${DATA}/g" mail.tpl
and also sed replace with variable with multiple lines. None of them work.
现在的问题是,如何让 sed
用多行文本替换某些内容?
Now the question is, how do I get sed
to replace something with multiple lines of text?
(也欢迎使用 sed
的替代品!)
(Alternatives to sed
are also welcome!)
推荐答案
如果您使用 "
" 构建多行文本,这将适用于简单的 sed代码>命令为:
If you build your multiple line text with "
"s, this will work with a simple sed
command as:
DATA=`echo ${DATA} | tr '
' "\n"`
#now, DATA="line1
line2
line3"
sed "s/_data_/${DATA}/" mail.tpl
这篇关于使用 sed 用多行替换一个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!