SED与多行变量替换 [英] sed replace with variable with multiple lines

查看:334
本文介绍了SED与多行变量替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与跨越多行的文本替换词。我知道我可以简单地使用换行符\\ n来解决这个问题,但我想保持字符串任何不需要格式化的干净。

I am trying to replace a word with a text which spans multiple lines. I know that I can simply use the newline character \n to solve this problem, but I want to keep the string "clean" of any unwanted formatting.

下面的例子显然是行不通的:

The below example obviously does not work:

read -r -d '' TEST <<EOI
a
b
c
EOI

sed -e "s/TOREPLACE/${TEST}/" file.txt

如何实现这一点没有改变它与读开头和结尾EOI?部分任何想法

Any ideas of how to achieve this WITHOUT modifying the part which starts with read and ends with EOI?

推荐答案

这是有趣的问题。

这可以让你更接近的解决方案为您的使用情况。

This may get you closer to a solution for your use case.

read -r -d '' TEST <<EOI
a\\
b\\
c
EOI

echo TOREPLACE | sed -e "s/TOREPLACE/${TEST}/"
a
b
c

我希望这有助于。

I hope this helps.

这篇关于SED与多行变量替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆