SED / AWK / bash的两个字符串与外部文件的内容之间的替换文本 [英] sed/awk/bash to replace text between two strings with external file contents

查看:152
本文介绍了SED / AWK / bash的两个字符串与外部文件的内容之间的替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待写一个脚本/命令,即会采取inputFile1,寻找在它特定的开始和结束串,并替换它们之间的所有文本
与inputFile2的全部内容。

在理想情况下,但不是强制性的,这应该工作,而不需要特殊字符转义,这样我就可以把变量的字符串被调用的脚本(这样我可以很容易地多次重复使用)。

作为一个例子,我有文件的 inputYes.txt 的内容有:

  DummyOne
开始

什么

结束
DummyTwo

inputNo.txt 的内容有:

 
为什么

和我想的脚本来搜索的 inputYes.txt 的为字符串的开始结束的,并用替换之间的所有文本内容的 inputNo.txt 的,并写入文件中。

所以它运行后, inputYes.txt 的应该读

  DummyOne
开始
这个
为什么
没有
结束
DummyTwo


解决方案

  sed的'/ end_string / rinputFile2
/ start_string /,/ end_string / D'inputFile1

I'm looking to write a script/command, that'll take inputFile1, look for a specific start and end string in it, and replace all the text in between them with the full contents of inputFile2.

Ideally, but not mandatory, this should work without a need to escape special characters, so I can put the strings in variables that get called by the script (that way I could easily reuse it multiple times).

As an example, I have file inputYes.txt with contents:

DummyOne
Start
That
What
Yes
End
DummyTwo

And inputNo.txt with contents:

This
Why
Not

And I want the script to search inputYes.txt for the strings Start and End, and replace all the text in between with the contents of inputNo.txt, and write to the file.

So after running it, inputYes.txt should read

DummyOne
Start
This
Why
No
End
DummyTwo

解决方案

sed '/end_string/rinputFile2
/start_string/,/end_string/d' inputFile1

这篇关于SED / AWK / bash的两个字符串与外部文件的内容之间的替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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