我正在尝试替换文件中的字符串,如下所示:
c:>powershell -Command (gc D:xxxxxxxindex.html) ^
-replace '^<script src="js/locale/languageholder.js"^>^</script^>', '' ^
| Out-File -encoding ASCII "D:yyyyyyyyyindex.html"
我要删除的字符串是:
<script src="js/locale/languageholder.js"></script>
没有错误,但它只是没有被替换。我想一定有一些人物逃脱了,但我想不通。
在代码^中用于转义<,否则将显示此错误:
< was unexpected at this time.
使用\逃离时出现相同错误
您需要-在命令行解析过程中,在PowerShell CLI剥离未转义的
"
字符(如果有的话(后,将要通过传递的所有"
字符转义到最终执行的PowerShell命令:
powershell -Command (gc D:xxxxxxxindex.html) ^
-replace '^<script src="js/locale/languageholder.js"^>^</script^>', '' ^
| Out-File -encoding ASCII "D:yyyyyyyyyindex.html"
有关详细信息,请参阅此答案。
最简单的方法是拥有一个外部powershell文件:
replace.ps1:
(gc index.html) -replace '<script src="js/locale/languageholder.js"></script>' |
set-content index2.html
然后运行:
powershell -file replace.ps1