Suppose I have the following data in read.txt:
_app1_ip_
_app2_ip_
_app1_ip_
_app3_ip_
_app2_ip_
And I want to replace each with a certain corresponding value (in this case, the values in 'list') and output that to another file (out.txt):
list = ['app1', 'app2', 'app3']
for l in list:
field = '_%s_ip_' %l
patterns = {
field : l,
}
with open("read.txt", "r") as my_input:
content = my_input.read()
with open("out.txt", "w+") as my_output:
for i,j in patterns.iteritems():
content = content.replace(i,j)
my_output.write(content)
What I want is the following in data.txt:
app1
app2
app1
app3
app2
What I actually get is:
_app1_ip_
_app2_ip_
_app1_ip_
app3
_app2_ip_
This seems so simple.. would be an easy one-liner in bash/sed. Can anyone please help/explain?