如何使用标准Linux工具将`---`-separated块替换为文本文件的内容?

时间:2015-06-29 13:52:14

标签: linux awk sed

我有一个看起来像这样的文本文件:

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

我要做的是将--- - 划分的块替换为文本文件的内容,以便它看起来像这样:

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,但我认为不适用,因为我所处理的是一个多行块,看起来sed似乎不是很擅长那个。可以awkruby或其他内容吗?

1 个答案:

答案 0 :(得分:4)

未经测试,但如果不是您想要的话,将会关闭:

awk '
NR==FNR { file1 = file1 $0 RS; next }
/---/ {
    if (f) {
        printf "%s", file1
    }
    f = !f
    next
}
!f
' file1 file2