替换文件中的小块html文本

时间:2016-04-05 11:56:33

标签: windows powershell batch-file powershell-v2.0

我一直在寻找答案,但我无法解决。 在我的情况下,我需要替换.htm文件中的字符串。字符串跨多行,例如:

</table>
    <p>
<table border="1">

我尝试了以下但是它不起作用,我知道因为文本显示在几行上。

Get-Content test1.htm )| ForEach{ $_ -replace '</table><p><table border="1">', "" } | Set-Content test2.htm

我不知道我该怎么做。

2 个答案:

答案 0 :(得分:0)

试试这个:

(Get-Content 'filename').replace('toreplace', 'replacewith') | Set-Content 'filename'

答案 1 :(得分:0)

好的,有人设法解决了,所以我发布了答案。 cule包括Poweshell溢出操作符: \ s +

所以我的最终代码如下:

$filenames = @("test1.htm")foreach ($file in $filenames) {
$replacementStr = ''
(Get-Content $file | Out-String) | 
    Foreach-object { $_ -replace '</table>.\s+<p>\s+<table border="1">' , $replacementStr } | 
 Set-Content test2.htm }
相关问题