我自动化部署过程,我需要一个工具来替换文件的一部分。我需要替换的部分是这样的:
Vari = {
...
}
REPLACEME = {
'default' = {
...
}
}
...
就像你可以看到的,它是一个嵌套数组(但我知道它只有1个嵌套数组)。我有代码替换名为FILETOREPLACE.txt的文件中的数组,我需要用该文件的内容替换结构REPLACEME。
使用sed,我得到一个定位结构的脚本(它删除它),但是我无法用文件的内容替换它。 sed脚本是下一个:
sed '/REPLACEME = {/{:1; /}/!{N; b1}; N; :2; /}/!{N; b2};//d};' settings.py
这个sed脚本实现了这个:
'/REPLACEME = {/{ -> It matchs the begining of the array
:1; -> Tag to the parents array
/}/!{ -> If it's not a closing curl brace
N; -> Read next
b1 -> Go back to :1;
};
N; -> If its the closing curl braces, continue
:2; -> Next tag
/}/!{ -> Again... continue until it finds a closing curl brace
N; -> ...
b2 -> ...
};
//d}; -> This deletes the buffer (I have not been able to replace it)
所以...我想如果有人可以更正我的脚本以将匹配替换为另一个文件的内容(称为FILETOREPLACE.txt)
答案 0 :(得分:1)
这样的事情可能会:
sed '/REPLACEME/rFILETOREPLACE.txt
/REPLACEME/,/^\}/d' sed.in
第一个语句在找到FILETOREPLACE.txt
后立即将文件REPLACEME
的内容插入到流中,第二个语句只删除从REPLACEME
到该块结尾的所有内容。请注意,我通过假设块以从右括号开始的第一行结束来简化删除。