2013-07-30 66 views
0

我有页眉和页脚像一个XML文件:XML字符串替换在Windows批处理文件

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 "[email protected]%header%@@g" -i.backup xmlFile.xml any suggestions? 
+0

你的代码中有不同的变量:'header'和'xmlHeader'。 – Endoro

+0

更改为变量为相同。但这不是问题。脚本仍然不会从xml文件中移除标题。 – Evan

回答

3

在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 "[email protected]%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 "[email protected]<?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> 

注意:此处不需要seds命令的g标志。

+0

很棒!感谢Endoro! – Evan

相关问题