在 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