import os,re
tt2=open('/etc/hosts','r')
for s in tt2:
#ip hostname pattern as \d+.\d+.\d+.\d \d means number
matchObj = re.match( r'\d+.\d+.\d+.\d+\t\w+',s)
if matchObj:
print "matchObj.group() : "matchObj.group().split('\t')
搜尋此網誌
2013年3月22日 星期五
2013年3月20日 星期三
PARAMETER PARSE
import platform,sys,os,fileinput,re
global dstconfig,org_pattern,chg_pattern,srcconfig,org_pattern
srcconfig=''
org_pattern=''
dstconfig=''
org_pattern=''
patternfs="/Users/Hermes/Downloads/srt.txt"
def get_ip():
bond1_dst='/etc/sysconfig/network-scripts/ifcfg-bond1'
eth1_dst='/etc/sysconfig/network-scripts/ifcfg-eth1'
if os.path.isfile(bond0_dst) == True:
temp=open(bond1_dst,'r')
text=temp.read()
ip_tmp=str(re.findall('IPADDR=\d+.\d+.\d+.\d+',text))
ip=ip_tmp.rsplit('IPADDR=')
elif os.path.isfile(bond1_dst) == False and os.path.isfile()==True:
temp=open(eth01_dst,'r')
text=temp.read()
ip_tmp=str(re.findall('IPADDR=\d+.\d+.\d+.\d+',text))
ip=ip_tmp.rsplit('IPADDR=')
else:
print('some wrong')
def change_file(srcconfig,org_pattern,chg_pattern):
changing_file_dst='/Users/Hermes/Downloads/srt.txt'
changefile=fileinput.input(srcconfig, inplace=1)
#results=re.sub(stringa,replace,changefile)
result=st.replace(pattern,replace_str)
changefile.close()
def paramter_parse(patternfs):
ofs=open(patternfs)
for s in ofs:
parameter=tuple(s.split('::'))
print(parameter)
print(range(len(parameter)))
if len(parameter)
2013年3月17日 星期日
How to use python replace a word as sed does
import os,fileinput
changing_file_dst='/Users/Hermes/Downloads/srt.txt' changefile=fileinput.input(changing_file_dst, inplace=1)
# <==this is part very import if you just fileinput.input(change_file_dst) data will not modified , so you need set inplace=1 to modify current file
for st in changefile:
replace_str='cluster.enabled=true'
pattern='cluster.enabled=false'
result=st.replace(pattern,replace_str)
print(result) changefile.close()
original I just want use open , but when I write all file will be truncate and write . so I use fileinput.
原本是用open 將檔案 打開修改可是用 W mode 會因為先作truncated , 再寫入所以檔案會被清空 ,所以改用 fileinput 直接作修改
changing_file_dst='/Users/Hermes/Downloads/srt.txt' changefile=fileinput.input(changing_file_dst, inplace=1)
# <==this is part very import if you just fileinput.input(change_file_dst) data will not modified , so you need set inplace=1 to modify current file
for st in changefile:
replace_str='cluster.enabled=true'
pattern='cluster.enabled=false'
result=st.replace(pattern,replace_str)
print(result) changefile.close()
original I just want use open , but when I write all file will be truncate and write . so I use fileinput.
原本是用open 將檔案 打開修改可是用 W mode 會因為先作truncated , 再寫入所以檔案會被清空 ,所以改用 fileinput 直接作修改
訂閱:
文章 (Atom)