我正在尝试用另一个文件中的内容替换文件中的多行模式匹配。
例如:
文件1.xml
<root>
<child>
<subchild>a</subchild>
</child>
</root>
文件2.xml
<child>
<subchild>a</subchild>
<subchild>b</subchild>
<subchild>c</subchild>
</child>
将
<clild> </child>
中的整个 file1.xml
标签替换为 file2.xml
输出:
<root>
<child>
<subchild>a</subchild>
<subchild>b</subchild>
<subchild>c</subchild>
</child>
</root>
我已经尝试使用 sed 和 /r 并读取 xmlstarlet,但无法使其正常工作。任何指针。 TIA
这可能对你有用(GNU sed):
sed -e '1i/<child>/,/<\\/child>/c\\' -e '$!s/$/\\/' file2 | sed -f - file1
使用 file2 编写 sed 脚本。
使用从
<child>
到 </child>
的范围将匹配更改为所需的结果。
替代方案:
sed -e '/<child>/{:a;N;/<\/child>/!ba;r file2' -e 'd}' file1
有这样一个怪物:
(
echo '<w><t>'
cat file1.xml
echo '</t><n>'
cat file2.xml
echo '</n></w>'
) |
xmlstarlet ed -O \
-m /w/t/root/child/following-sibling::'*' /w/n \
-d /w/t/root/child \
-m /w/n/'*' /w/t/root \
-m /w/t/root / \
-d /w'[1]'