Reputation: 6764
I have some file:
aaaaaaaaaa
# Lines Start #
bbbbbbbbb
cccccccc
dddddddddd
# Lines End #
eeeeeeeeee
And some variable $newLines="new line"
How can I replace the lines from # Lines Start #
to # Lines End #
(including those line themselves) with variable's value using sed.
so I'll get:
aaaaaaaaaa
new line
eeeeeeeeee
I've tried using: sed -E 'N; s/# Lines Start #\.*\# Lines End #/$newLines/'
But its not doing it's job.
Upvotes: 1
Views: 81
Reputation: 58420
This might work for you (GNU sed):
var='first line\
second line\
third'
# N.B. multilines must end in "\" except for the last line
sed '/# Lines Start #/,/# Lines End #/c\'"$var" file
Upvotes: 1
Reputation: 77105
Here is one way with sed
:
sed '/# Lines Start #/,/# Lines End #/{
/# Lines End #/!d
s/# Lines End #/new line/
}' file
aaaaaaaaaa
new line
eeeeeeeeee
new line
Upvotes: 2
Reputation: 41456
You can use awk
if you like to try it:
awk '/# Lines Start #/ {f=1} !f; /# Lines End #/ {print "new line";f=0}' file
aaaaaaaaaa
new line
eeeeeeeeee
You can also use range like in sed
, but its less flexible, so I normal avoid the range function.
awk '/# Lines Start #/,/# Lines End #/ {if (!a++) print "new line";next}1' file
aaaaaaaaaa
new line
eeeeeeeeee
Upvotes: 2