Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 919
Eric Brotto Avatar asked Jul 22 '11 13:07

Eric Brotto


People also ask

Does sed overwrite file?

By default sed does not overwrite the original file; it writes to stdout (hence the result can be redirected using the shell operator > as you showed).

Which sed command is used for replacement?

Replacing all the occurrence of the pattern in a line : The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line. Output : linux is great os.

How do you replace a line in a file using bash?

The 'sed' command is used to replace any string in a file using a bash script. This command can be used in various ways to replace the content of a file in bash. The 'awk' command can also be used to replace the string in a file.


2 Answers

You can use the r command. When you find a 'fox' in the input...

/fox/{ 

...replace it for nothing...

    s/fox//g 

...and read the input file:

    r f.html } 

If you have a file such as:

$ cat file.txt the quick brown fox jumps over the lazy dog fox dog 

the result is:

$ sed '/fox/{     s/fox//g     r f.html }' file.txt the quick brown      </div>   </div>   <br>   <div id="container2">     <div class="question" onclick="javascript:show('answer2')";> jumps over the lazy dog  dog     </div>   </div>   <br>   <div id="container2">     <div class="question" onclick="javascript:show('answer2')";> 

EDIT: to alter the file being processed, just pass the -i flag to sed:

sed -i '/fox/{     s/fox//g     r f.html }' file.txt 

Some sed versions (such as my own one) require you to pass an extension to the -i flag, which will be the extension of a backup file with the old content of the file:

sed -i.bkp '/fox/{     s/fox//g     r f.html }' file.txt 

And here is the same thing as a one liner, which is also compatible with Makefile

sed -i -e '/fox/{r f.html' -e 'd}' 
like image 136
brandizzi Avatar answered Oct 12 '22 09:10

brandizzi


Ultimately what I went with which is a lot simpler than a lot of solutions I found online:

str=xxxx sed -e "/$str/r FileB" -e "/$str/d" FileA 

Supports templating like so:

str=xxxx sed -e "/$str/r $fileToInsert" -e "/$str/d" $fileToModify 
like image 41
buddyp450 Avatar answered Oct 12 '22 08:10

buddyp450