繁体   English   中英

如何使用标准的Linux工具用文本文件的内容替换`---'分隔的块?

[英]How can I replace a `---`-separated block with the contents of a text file, using standard Linux tools?

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

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. 

这类似于这个问题 ,但是我不认为这适用,因为我要处理的是多行块,而sed似乎并不擅长于此 awkruby可以这样做吗?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM