linux - 如何使用标准 Linux 工具将 `---` 分隔的 block 替换为文本文件的内容?

标签 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

我想做的是用文本文件的内容替换 ----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 似乎不太擅长这一点。 awkruby 或其他东西可以做到这一点吗?

最佳答案

未经测试,但如果不是您想要的,将接近:

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/

相关文章:

bash - 使用sed,如何进一步转换group back引用?

linux - 无法通过 SSH 在远程服务器上安装 Dropbox

regex - 如何替换与 egrep 匹配的较大字符串中的子字符串?

linux - 使用另一个文件的行号从文本文件中删除行

regex - 如何使用 sed 按正则表达式对行进行排序

linux - 如何在 vi 编辑器或 linux 中查找和删除两个文件中的重复行

linux - 无法制定与 sed 一起使用的正则表达式来提取列值

linux - 在期望脚本中发送不可打印的字符

linux - 自定义命令创建

Linux 原始套接字 (layer2) - 最佳策略