2013-07-30 53 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

相關問題