我有一个看起来像这样的文本文件:
Some text here. This text is not replaced.
---
And then a wild block appears!
It has stuff in it that I'm trying to replace.
---
The block is no more. Nothing to replace here.
还有另一个要插入内容的文本文件:
A multi-
line thing to replace.
This block is not demarcated
in the same way
as the other
我想做的是用文本文件的内容替换 ---
-demarcated block ,这样它看起来像这样:
Some text here. This text is not replaced.
A multi-
line thing to replace.
This block is not demarcated
in the same way
as the other
The block is no more. Nothing to replace here.
这类似于 this question ,但我认为这不适用,因为我正在处理的是一个多行 block ,而且 sed
似乎不太擅长这一点。 awk
或 ruby
或其他东西可以做到这一点吗?
最佳答案
未经测试,但如果不是您想要的,将接近:
awk '
NR==FNR { file1 = file1 $0 RS; next }
/---/ {
if (f) {
printf "%s", file1
}
f = !f
next
}
!f
' file1 file2
关于linux - 如何使用标准 Linux 工具将 `---` 分隔的 block 替换为文本文件的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31117489/