Use the contents of a file to replace a string using SED

Viewed 29921

What would be the sed command for mac shell scripting that would replace all iterations of string "fox" with the entire string content of myFile.txt.

myFile.txt would be html content with line breaks and all kinds of characters. An example would be

    </div>
  </div>
  <br>
  <div id="container2">
    <div class="question" onclick="javascript:show('answer2')";>

Thanks!

EDIT 1

This is my actual code:

sed -i.bkp  '/Q/{
s/Q//g
r /Users/ericbrotto/Desktop/question.txt
}' $file

When I run it I get:

sed in place editing only works for regular files. 

And in my files the Q is replaced by a ton of chinese characters (!). Bizarre!

3 Answers

Another method (minor variation to other solutions):

If your filenames are also variable ( e.g. $file is f.html and the file you are updating is $targetfile): sed -e "/fox/ {" -e "r $file" -e "d" -e "}" -i "$targetFile"

Related