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
.
.
.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityThis 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 w
rite command, and the q
uit 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.