0

Original file

.
.
. 
startpattern
   text to copy 1
   test to copy 2
endpattern
.
.
.
thirdpattern

Result file

.
.
. 
startpattern
   text to copy 1
   test to copy 2
endpattern
.
.
.
thirdpattern
   text to copy 1
   test to copy 2
.
.
.

1 Answer 1

2

This seems pretty straight-forward with the scriptable editor ed. The core command is:

/startpattern/+1, /endpattern/-1 t /thirdpattern/

You could change the file with the command-line:

printf '%s\n' '/startpattern/+1, /endpattern/-1 t /thirdpattern/' w q | ed -s filename

... which pipes three commands into ed's input buffer: the main command, the write command, and the quit command.

You would/should tighten the regular expressions on each of the patterns to be sure that you're capturing the correct lines. The +1 and -1 on the first two patterns adjust the match forwards and backwards to exclude the lines with those two patterns.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .