查找HTML字符串和备注

时间:2012-12-08 13:44:32

标签: regex batch-file sed

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

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

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

CODE:

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%"

谢谢

1 个答案:

答案 0 :(得分:0)

如果我正确知道sed,则换行符应为\ n,以便sed处理换行符而不是批处理文件;并且字符串中的正斜杠需要通过反斜杠进行转义。每当角色\ /&amp;从字面上看,它们需要被转义。此外,您的代码段中没有使用延迟扩展功能,因此我删除了它。

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%"

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