sed/awk/bash to replace text between two strings w

2020-07-26 10:47发布

问题:

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
Not
End
DummyTwo

回答1:

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