January 27th, 2013, 09:51 AM
Python: line replacement
How to replace line # 10 in a file, append a line at the end of the file ?
January 27th, 2013, 10:29 AM
I found the solution and tested this successfully on local system but when i used fabric module to deploy in remote system, it modify the local hosts file and not remote. where i am wrong ? no errors reported.
[root@lb1oraoemc1p hp]# cat fabfile.py
from fabric.api import cd,env,put,run
env.hosts=['root@xyz'] # remote hostname xyz
f = open('/etc/default/xymon-client')
m = f.read().replace('XYMONSERVERS=""','XYMONSERVERS=aaa.xyz.com')
f = open('/etc/default/xymon-client','w')
with open("/etc/default/xymon-client","a") as myfile:
run('service xymon start')
January 27th, 2013, 01:36 PM
I'd use ed . The script is
$ ed file_to_change
10s/.*/replacement text for line 10/
text appended to file
more appended text
[/code] are essential for python code and Makefiles!