sed 正则表达式匹配一行开头,整行替换,替换内容中有反斜杠
要替换test.txt文件中以 -period开头的一行
。
create_clock \
-period 1234567890 \
-name master_clk \
clk
要把第二行替换为
-period abc \
可采用sed命令直接替换:
sed -i '/^ *-period/c\-period abc \\ ' test.txt
替换后:
create_clock \
-period abc \
-name master_clk \
clk
如果在python脚本中写,转义比较麻烦:
cmd='sed -i \'/^ *-period/c\\-period 1000\\\ \' test.txt'
os.system(cmd)