Replace all lines between two patterns (inclusive) with a file content

Clash Royale CLAN TAG#URR8PPP up vote
3
down vote
favorite
All is said in the title , I want to replace all lines between two patterns with a a file content.
file1
line 1
line 2
foo
foobar
bar
line 6
line 7
file2
line 3
line 4
line 5
desired file
line 1
line 2
line 3
line 4
line 5
line 6
line 7
Tried many sed commands nothing works for me , closest command below successfully matchs lines between foo and bar but replace them with "$(cat file2)" string and not file content.
sed '/foo/:a;N;/bar/!ba;N;s/.*n/$(cat file2)/;p' file1
command-line text-processing sed regex
add a comment |Â
up vote
3
down vote
favorite
All is said in the title , I want to replace all lines between two patterns with a a file content.
file1
line 1
line 2
foo
foobar
bar
line 6
line 7
file2
line 3
line 4
line 5
desired file
line 1
line 2
line 3
line 4
line 5
line 6
line 7
Tried many sed commands nothing works for me , closest command below successfully matchs lines between foo and bar but replace them with "$(cat file2)" string and not file content.
sed '/foo/:a;N;/bar/!ba;N;s/.*n/$(cat file2)/;p' file1
command-line text-processing sed regex
Here is a ugly workaround:printf '%sn' "$(sed '/^foo$/,$d' file1 && cat file2 && sed -e '/^bar$/,$!d' -e '/^bar$/d' file1)" > desired-fileor(sed '/^foo$/,$d' file1 && cat file2 && sed -e '/^bar$/,$!d' -e '/^bar$/d' file1) > desired-file
â pa4080
Jan 29 at 17:39
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
All is said in the title , I want to replace all lines between two patterns with a a file content.
file1
line 1
line 2
foo
foobar
bar
line 6
line 7
file2
line 3
line 4
line 5
desired file
line 1
line 2
line 3
line 4
line 5
line 6
line 7
Tried many sed commands nothing works for me , closest command below successfully matchs lines between foo and bar but replace them with "$(cat file2)" string and not file content.
sed '/foo/:a;N;/bar/!ba;N;s/.*n/$(cat file2)/;p' file1
command-line text-processing sed regex
All is said in the title , I want to replace all lines between two patterns with a a file content.
file1
line 1
line 2
foo
foobar
bar
line 6
line 7
file2
line 3
line 4
line 5
desired file
line 1
line 2
line 3
line 4
line 5
line 6
line 7
Tried many sed commands nothing works for me , closest command below successfully matchs lines between foo and bar but replace them with "$(cat file2)" string and not file content.
sed '/foo/:a;N;/bar/!ba;N;s/.*n/$(cat file2)/;p' file1
command-line text-processing sed regex
command-line text-processing sed regex
edited Jan 29 at 17:38
pa4080
12.4k52358
12.4k52358
asked Jan 29 at 16:51
storm
3,82032032
3,82032032
Here is a ugly workaround:printf '%sn' "$(sed '/^foo$/,$d' file1 && cat file2 && sed -e '/^bar$/,$!d' -e '/^bar$/d' file1)" > desired-fileor(sed '/^foo$/,$d' file1 && cat file2 && sed -e '/^bar$/,$!d' -e '/^bar$/d' file1) > desired-file
â pa4080
Jan 29 at 17:39
add a comment |Â
Here is a ugly workaround:printf '%sn' "$(sed '/^foo$/,$d' file1 && cat file2 && sed -e '/^bar$/,$!d' -e '/^bar$/d' file1)" > desired-fileor(sed '/^foo$/,$d' file1 && cat file2 && sed -e '/^bar$/,$!d' -e '/^bar$/d' file1) > desired-file
â pa4080
Jan 29 at 17:39
Here is a ugly workaround:
printf '%sn' "$(sed '/^foo$/,$d' file1 && cat file2 && sed -e '/^bar$/,$!d' -e '/^bar$/d' file1)" > desired-file or (sed '/^foo$/,$d' file1 && cat file2 && sed -e '/^bar$/,$!d' -e '/^bar$/d' file1) > desired-fileâ pa4080
Jan 29 at 17:39
Here is a ugly workaround:
printf '%sn' "$(sed '/^foo$/,$d' file1 && cat file2 && sed -e '/^bar$/,$!d' -e '/^bar$/d' file1)" > desired-file or (sed '/^foo$/,$d' file1 && cat file2 && sed -e '/^bar$/,$!d' -e '/^bar$/d' file1) > desired-fileâ pa4080
Jan 29 at 17:39
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You are close, I think - your main issue is that $(cat file2) is going to be treated as literal within single quotes - you should be using the built-in r command:
r filename
Queue the contents of filename to be read and inserted into the output
stream at the end of the current cycle, or when the next input line is
read. Note that if filename cannot be read, it is treated as if it
were an empty file, without any error indication.
So:
sed '
/foo/
:a
N
/nbar$/!ba
r file2
d
' file1
If you want to wrangle this into a one-liner there's a trick you'll need to prevent sed from treating everything after the r as part of the filename:
sed -e '/foo/:a; N; /nbar$/!ba; r file2' -e 'd;' file1
+1 as it works for the generic example above .. God knows why it didn't work for my xml files.. managed to find another way to solve my problem though thanks for helping.
â storm
Jan 31 at 14:04
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You are close, I think - your main issue is that $(cat file2) is going to be treated as literal within single quotes - you should be using the built-in r command:
r filename
Queue the contents of filename to be read and inserted into the output
stream at the end of the current cycle, or when the next input line is
read. Note that if filename cannot be read, it is treated as if it
were an empty file, without any error indication.
So:
sed '
/foo/
:a
N
/nbar$/!ba
r file2
d
' file1
If you want to wrangle this into a one-liner there's a trick you'll need to prevent sed from treating everything after the r as part of the filename:
sed -e '/foo/:a; N; /nbar$/!ba; r file2' -e 'd;' file1
+1 as it works for the generic example above .. God knows why it didn't work for my xml files.. managed to find another way to solve my problem though thanks for helping.
â storm
Jan 31 at 14:04
add a comment |Â
up vote
2
down vote
accepted
You are close, I think - your main issue is that $(cat file2) is going to be treated as literal within single quotes - you should be using the built-in r command:
r filename
Queue the contents of filename to be read and inserted into the output
stream at the end of the current cycle, or when the next input line is
read. Note that if filename cannot be read, it is treated as if it
were an empty file, without any error indication.
So:
sed '
/foo/
:a
N
/nbar$/!ba
r file2
d
' file1
If you want to wrangle this into a one-liner there's a trick you'll need to prevent sed from treating everything after the r as part of the filename:
sed -e '/foo/:a; N; /nbar$/!ba; r file2' -e 'd;' file1
+1 as it works for the generic example above .. God knows why it didn't work for my xml files.. managed to find another way to solve my problem though thanks for helping.
â storm
Jan 31 at 14:04
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You are close, I think - your main issue is that $(cat file2) is going to be treated as literal within single quotes - you should be using the built-in r command:
r filename
Queue the contents of filename to be read and inserted into the output
stream at the end of the current cycle, or when the next input line is
read. Note that if filename cannot be read, it is treated as if it
were an empty file, without any error indication.
So:
sed '
/foo/
:a
N
/nbar$/!ba
r file2
d
' file1
If you want to wrangle this into a one-liner there's a trick you'll need to prevent sed from treating everything after the r as part of the filename:
sed -e '/foo/:a; N; /nbar$/!ba; r file2' -e 'd;' file1
You are close, I think - your main issue is that $(cat file2) is going to be treated as literal within single quotes - you should be using the built-in r command:
r filename
Queue the contents of filename to be read and inserted into the output
stream at the end of the current cycle, or when the next input line is
read. Note that if filename cannot be read, it is treated as if it
were an empty file, without any error indication.
So:
sed '
/foo/
:a
N
/nbar$/!ba
r file2
d
' file1
If you want to wrangle this into a one-liner there's a trick you'll need to prevent sed from treating everything after the r as part of the filename:
sed -e '/foo/:a; N; /nbar$/!ba; r file2' -e 'd;' file1
edited Jan 30 at 1:47
answered Jan 29 at 17:13
steeldriver
63.6k1199168
63.6k1199168
+1 as it works for the generic example above .. God knows why it didn't work for my xml files.. managed to find another way to solve my problem though thanks for helping.
â storm
Jan 31 at 14:04
add a comment |Â
+1 as it works for the generic example above .. God knows why it didn't work for my xml files.. managed to find another way to solve my problem though thanks for helping.
â storm
Jan 31 at 14:04
+1 as it works for the generic example above .. God knows why it didn't work for my xml files.. managed to find another way to solve my problem though thanks for helping.
â storm
Jan 31 at 14:04
+1 as it works for the generic example above .. God knows why it didn't work for my xml files.. managed to find another way to solve my problem though thanks for helping.
â storm
Jan 31 at 14:04
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1001057%2freplace-all-lines-between-two-patterns-inclusive-with-a-file-content%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Here is a ugly workaround:
printf '%sn' "$(sed '/^foo$/,$d' file1 && cat file2 && sed -e '/^bar$/,$!d' -e '/^bar$/d' file1)" > desired-fileor(sed '/^foo$/,$d' file1 && cat file2 && sed -e '/^bar$/,$!d' -e '/^bar$/d' file1) > desired-fileâ pa4080
Jan 29 at 17:39