import re,sys
spec_file=sys.argv[1]
spec=''
with open(spec_file,'r') as sp:
spec=sp.read()
#print(spec)
pattern = re.compile(r'%package base.+(?=%package mgr\s)',re.S)
pattern1 = re.compile(r'%package fuse.+(?=%prep\s)',re.S)
pattern2 = re.compile(r'%files base.+(?=%files mgr\s)',re.S)
pattern3 = re.compile(r'%files mon.+(?=%changelog\s)',re.S)
pattern4 = re.compile(r'\s(?=make "\$CEPH_MFLAGS_JOBS"\s)',re.S)
pattern5 = re.compile(r'(?<=%install)',re.S)
pattern6 = re.compile(r'pushd build',re.S)
content = pattern.sub('', spec)
content = pattern1.sub('', content)
content = pattern2.sub('', content)
content = pattern3.sub('', content)
content = pattern4.sub('cd src/mgr\n', content)
content = pattern5.sub('\n%define _unpackaged_files_terminate_build 0 \n', content)
content = pattern6.sub('pushd build/src/mgr', content)
match=pattern6.findall(spec)
with open(spec_file,'w') as sp:
sp.write(content)
re.compile(r’%package base.+(?=%package mgr\s)’,re.S) 中re.S设置符号 . 可以匹配空白符,(?=%package mgr\s)匹配到%package mgr前面,如果是匹配str后面是(?<=str)
shell实现截取字符串中间
main_pid=$(systemctl status ceph-mgr@node1 |awk '{match($0,/Main PID:\s+([0-9]+)\s+\(ceph-mgr\)/,a); print a[1]}' |grep -o -E '[0-9]+')