Let's say I have a file called original.txt
with this content:
red
blue
water
food
tree
gray
white
Also I have a file called new.txt
with this content:
green
black
yellow
purple
Now I want to write a script that replaces the lines between blue
and gray
in original.txt
with the contents of new.txt
, so it gives me this result:
red
blue
green
black
yellow
purple
gray
white
I wrote this piece of code for the purpose (the name of the new file is not always the same so it's stored in a variable):
newtext="new.txt"
sed -i "/blue/,/gray/{
r $newtext
d
}" original.txt
However, when running it I get this nonsense instead:
red
green
black
yellow
purplegreen
black
yellow
purplegreen
black
yellow
purplegreen
black
yellow
purplegreen
black
yellow
purplewhite
What am I doing wrong?