[英]XML string replacement in Windows batch file
我有一个带有页眉和页脚的xml文件,例如:
set "header=<?xml version="1.0" encoding="UTF-8"?><Config xmlns="http://namespace.com/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">"
set "footer=</Config>"
我想从xml文件中删除它们,然后读入页眉和页脚标记之间的其余项。
我尝试使用sed。 这在Linux上有效,但在Windows上却无能为力
sed -e "s@%header%@@g" -i.backup xmlFile.xml any suggestions?
在Windows中,必须用反斜杠转义双引号:
@ECHO OFF &SETLOCAL
set "XMLheader=<?xml version=\"1.0\" encoding=\"UTF-8\"?><Config xmlns=\"http://namespace.com/config\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.0\">"
set "footer=</Config>"
sed "s@%xmlHeader%\|%footer%@@g" file
命令行示例:
>type file
<?xml version="1.0" encoding="UTF-8"?><Config xmlns="http://namespace.com/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
<tag1>info1 changes in the loop</tag1>
<tag2>info2 changes in the loop</tag2>
</Config>
>sed "s@<?xml version=\"1.0\" encoding=\"UTF-8\"?><Config xmlns=\"http://namespace.com/config\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.0\">\|</Config>@@g" file
<tag1>info1 changes in the loop</tag1>
<tag2>info2 changes in the loop</tag2>
注: g
为标志s
的命令sed
是这里没有必要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.