3

Lets say I have fileA, with the contents

Hello, this is some
random text REPLACEHERE and some more
random text

and fileB with the contents

stuff that goes into
fileA, at that specific place

How do I properly replace REPLACEHERE inside fileA, with the contents of fileB ? Exactly in that place, just as if I was doing a simple regex operation ?

The closest I've got was

sed -i '/REPLACEHERE/r fileB' fileA

which only ends up appending fileB in the line after REPLACEHERE was found, and that's no good. I need it to be replaced exactly in place (I thought that's what the i flag was for actually).

João Pinto Jerónimo
  • 9,586
  • 15
  • 62
  • 86

1 Answers1

2

It probably won't be that easy with sed only, I'd use a shell variable/command expansion:

sed -i "s/REPLACEHERE/$(cat fileB)/g" fileA

-i means that the changes will be saved to fileA, rather than printed to stdout.

Mind the slashes in fileB, by the way. If there are any, they will have to be escaped.

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175