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

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

问题描述

我正在尝试用跨多行的文本替换一个单词.我知道我可以简单地使用换行符 来解决这个问题,但我想保持字符串干净"的任何不需要的格式.

I am trying to replace a word with a text which spans multiple lines. I know that I can simply use the newline character 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

关于如何在不修改以 read 开头并以 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

我希望这会有所帮助.

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

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