Replacing a string by a file using Sed

The following command should work for what you want:

sed "s/this is test/$(cat bar)/" foo

If foo contain more then one line, then you can use:

sed "s/this is test/$(sed -e 's/[\&/]/\\&/g' -e 's/$/\\n/' bar | tr -d '\n')/" foo

or:

sed -e '/this is a test/{r bar' -e 'd}' foo

Source of the last two commands: Substitute pattern within a file with the content of other file

To make the change in foo file, use sed -i.


One way:

sed -e '/this is test/r bar' -e '/this is test/d' foo

Sample result:

$ cat bar
12
23
$ cat foo
ab
this is test
cd
this is test
ef
$  sed -e '/this is test/r bar' -e '/this is test/d' foo
ab
12
23
cd
12
23
ef

Tags:

Sed