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

The name of the pictureThe name of the pictureThe name of the pictureClash 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









share|improve this question























  • 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















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









share|improve this question























  • 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













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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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-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
















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











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





share|improve this answer






















  • +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











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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





share|improve this answer






















  • +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















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





share|improve this answer






















  • +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













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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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

















  • +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


















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Popular posts from this blog

Trouble downloading packages list due to a “Hash sum mismatch” error

How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

Cutting all the characters after the last /