How to replace a pattern in one file with the contents of another file using sed?
Suppose I have fileA
, with the content
Hello, this is some
random text REPLACEHERE and some more
random text
and fileB
with content
stuff that goes into
fileA, at that specific place
How to properly replace REPLACEHERE
inside fileA
, with content fileB
? Exactly at this point, as if I were doing a simple regex operation?
The closest I got
sed -i '/REPLACEHERE/r fileB' fileA
which ends up adding fileB to the line after it REPLACEHERE
was found and that's not good. I need it to be replaced exactly in place (I thought there was actually a flag i
).
Most likely it won't be that easy with sed
, I would use a shell / command extension:
sed -i "s/REPLACEHERE/$(cat fileB)/g" fileA
-i
means that changes will be saved to fileA
, not printed to stdout
.
Note the forward slashes in the fileB
. If they are, they must be screened.