這是test.txt的使用由在python多行替換單個線圖案SED
swift-ring-builder account.builder add zZONE-STORAGE_LOCAL_NET_IP:6002/DEVICE 100
我想用sed來替換上面在python多行線,使得上述線變得:
swift-ring-builder account.builder add z1-10.0.0.2:6002/sda 100
swift-ring-builder container.builder add z1-10.0.0.2:6001/sda 100
swift-ring-builder object.builder add z1-10.0.0.2:6000/sda 100
我用:
import subprocess
def search_and_replace_pattern(oldstr, newstr, path):
subprocess.call(['sed', '-i', 's#'+oldstr+'#'+newstr+'#', path])
但後來我得到:
sed: -e expression #1, char 88: unterminated `s' command
在我以前#
,因爲我的模式containes /
,所以我用了#
分隔符,使SED不/
混淆,請建議使用subprocess
或類似os.system(cmd)
爲什麼不使用Python中的「re」模塊呢? –
@MartijnPieters你好Martijn,我是新手,你可以指定如何使用're'模塊 – Capricorn
你的輸入將如何導致你的輸出是很不清楚的。你沒有包含你的'oldstr'和'newstr'的值。 –