Replace characters in a string from a list and create a copy for each
<p>I'm not sure if it's even possible to do what I need with sed.</p>

<p>I want to be able to replace a string in a file with another string from a list and create a separate copy for each version.</p>

<p>For example, the original file has:</p>

<pre><code>apple dog orange
</code></pre>

<p>I want to replace <code>dog</code> with a list of different words. i.e cat, rabbit, duck.
This would create copies of the original file, each with a different change.</p>

<p>File1.txt (the original file) - <code>apple dog orange</code>
File2.txt - <code>apple cat orange</code>
File3.txt - <code>apple rabbit orange</code>
File4.txt - <code>apple duck orange</code></p>

<p>I've been able to use Sed to find and replace a single file:</p>

<pre><code>sed -i '' 's/dog/cat/g' *.html
</code></pre>

<p>I need a way to replace from a list though, and create a unique copy of each.</p>

<p>I'm on OSX. As I said before, sed might not be the tool to use here, maybe Python? I'm open to any suggestions to do this.</p>

<p>Thanks</p>

<p>You can do it simply using string replace method</p>

<pre><code>import string
f = open("file1")
text = f.read()
list_of_words = ['cat','rabbit','duck']
num =2
for word in list_of_words:
    new_text = string.replace(text,"dog",word,1)
    f_new = open("file"+str(num),"w")
    f_new.write(new_text)
    f_new.close()
    num +=1
</code></pre>

<p>input: 
file1</p>

<pre><code>apple dog orange
</code></pre>

<p>output: file2:</p>

<pre><code>apple cat orange
</code></pre>

<p>output: file3:</p>

<pre><code>apple rabbit orange
</code></pre>

<p>output: file4:</p>

<pre><code>apple duck orange
</code></pre>

python string replace sed