0

I'm trying to

  1. Get a range in file_1 that matches the string 'start' and 'end' with regexp (assume there's only one 'start' and one 'end' in the whole file)
  2. Replace that multi-line range with the whole file_2

Note:

Found this example: SED: copy lines from a file to specific line in another file

And tried this: sed -i "" "/start/,/end/r<(sed '1,2!d' file_2)" file_1

And got this feedback in terminal: event not found: d

  • That's bash history search happening due to the `!`. Just googling that error message will tell you about it, e.g. the first hit I get is https://serverfault.com/questions/208265/what-is-bash-event-not-found. If you [edit] your question to include a [mcve] with concise, testable sample input and expected output then we can help you do whatever it is you're trying to do. See [ask] if that's not clear. – Ed Morton Sep 02 '21 at 01:37
  • In the explanation, you tall about "whole file_2" but in the code you try to use only the first two lines `sed '1,2!d' file_2` – Ronaldo Ferreira de Lima Sep 02 '21 at 03:16

1 Answers1

1

I think this can solve your problem:

sed -r '
/^start$/ {
    # After find 'start', append lines until reach 'end'
    :a
    N
    /\nend$/ {
        # remove whole block
        s/.*//
        # append file
        r file_2.txt
        d;
    }
    ba
}
' file_1.txt