0

我有一个 HTML 创建脚本,在其中我试图:

1.) 使用 SED 将 RelatedLinksBlock 的所有实例替换为多行 DOS 变量
2.) 查找与 DOS 变量匹配的字符串并使用 SED 将其移除或删除

几个小时后,我无法让任何一个 SED 命令工作,需要一些帮助。

代码:

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
set nl=^& echo.
set fullHTMLpath=http://this.com
set FileTitle=Computer TEST
set keyfile=test.html
set rempath=REMthisOUT
set "delpath=^<li^> - ^<a href="%fullHTMLpath%" title="%FileTitle%"^>%FileTitle%^</a^>^</li^>"
set "path1=^<li^> - ^<a href="http://that.com/computers-and-operating-systems/asus-computer-support.html" title="ASUS Computer Support"^>ASUS Computer Support^</a^>^</li^>"
set "path2=^<li^> - ^<a href="http://that.com/computers-and-operating-systems/asus-laptop-support.html" title="ASUS Laptop Support"^>ASUS Laptop Support^</a^>^</li^>"
set "path3=^<li^> - ^<a href="http://that.com/computers-and-operating-systems/asus-support.html" title="ASUS Support"^>ASUS Support^</a^>^</li^>"
set alllinks=%path1%%nl%%path2%%nl%%path3%

sed -i "s/RelatedLinksBlock/%alllinks%/g" "%keyfile%"
sed -i "s/%delpath%/%rempath%/g" "%keyfile%"

谢谢

4

1 回答 1

0

如果我知道 sed 正确,则换行符应该是 \n 以便 sed 处理换行符而不是批处理文件;并且字符串中的正斜杠需要用反斜杠转义。每当字符 \ / & 按字面意思使用时,都需要对其进行转义。在您的代码段中也没有使用延迟扩展,所以我删除了它。

SETLOCAL ENABLEEXTENSIONS
set "fullHTMLpath=http:\/\/this.com"
set "FileTitle=Computer TEST"
set "keyfile=test.html"
set "rempath=REMthisOUT"
set "delpath=^<li^> - ^<a href="%fullHTMLpath%" title="%FileTitle%"^>%FileTitle%^<\/a^>^<\/li^>"
set "path1=^<li^> - ^<a href="http:\/\/that.com\/computers-and-operating-systems\/asus-computer-support.html" title="ASUS Computer Support"^>ASUS Computer Support^<\/a^>^<\/li^>"
set "path2=^<li^> - ^<a href="http:\/\/that.com\/computers-and-operating-systems\/asus-laptop-support.html" title="ASUS Laptop Support"^>ASUS Laptop Support^<\/a^>^<\/li^>"
set "path3=^<li^> - ^<a href="http:\/\/that.com\/computers-and-operating-systems\/asus-support.html" title="ASUS Support"^>ASUS Support^<\/a^>^<\/li^>"
set "alllinks=%path1%\n%path2%\n%path3%"

sed -i "s/RelatedLinksBlock/%alllinks%/g" "%keyfile%"
sed -i "s/%delpath%/%rempath%/g" "%keyfile%"

除了这些建议之外,您是否收到任何可以向我们展示的错误消息?

于 2012-12-08T23:13:56.817 回答