Windows 替换文件中的小html文本块

Windows 替换文件中的小html文本块,windows,powershell,batch-file,powershell-2.0,Windows,Powershell,Batch File,Powershell 2.0,我已经寻找答案很多天了,但我就是解决不了。 在我的例子中,我需要替换.htm文件中的字符串。字符串跨越多行,例如: </table> <p> <table border="1"> 我尝试了以下方法,但不起作用,我知道,因为文本显示在几行上 Get-Content test1.htm )| ForEach{ $_ -replace '</table><p><table border="1">', "" } |

我已经寻找答案很多天了,但我就是解决不了。 在我的例子中,我需要替换.htm文件中的字符串。字符串跨越多行,例如:

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


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

Get-Content test1.htm )| ForEach{ $_ -replace '</table><p><table border="1">', "" } | Set-Content test2.htm
Get Content test1.htm)| ForEach{$|-replace',“”}| Set Content test2.htm
我不知道该怎么做。

试试这个:

(Get-Content 'filename').replace('toreplace', 'replacewith') | Set-Content 'filename'
试试这个:

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

好的,有人设法解决了这个问题,所以我发布了答案。cule将包括Poweshell Spill操作符:\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 }
$filenames=@(“test1.htm”)foreach($filenames中的文件){
$replacementStr=''
(获取内容$file |输出字符串)|
Foreach对象{$\-replace'.\s+\s+',$replacementStr}
设置内容test2.htm}

好的,有人设法解决了这个问题,所以我发布了答案。cule将包括Poweshell Spill操作符:\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 }
$filenames=@(“test1.htm”)foreach($filenames中的文件){
$replacementStr=''
(获取内容$file |输出字符串)|
Foreach对象{$\-replace'.\s+\s+',$replacementStr}
设置内容test2.htm}

是否始终替换相同的字符串?还是一种模式?另外,您希望用什么替换字符串?在这种情况下,我希望始终替换相同的strin,但对于将来,最好知道如何操作。我需要删除该字符串或使用另一个值重新计算,这取决于具体情况。您应该小心使用
-replace
cmdlet替换html文件中的字符串。您应该尝试使用XML解析器。看:如果你已经搜索了一段时间,你一定尝试了一些东西。你有没有我们可以帮助你的例子。这是一个基本的请求,已经有很多相关的答案。@Matt(Get Content test1.htm)| ForEach{$\u-replace',“”}| Set Content test2.htm但它不起作用,因为字符串是3行,比如:'\n\n',所以我只使用powershell进行了尝试。是否始终替换相同的字符串?还是一种模式?另外,您希望用什么替换字符串?在这种情况下,我希望始终替换相同的strin,但对于将来,最好知道如何操作。我需要删除该字符串或使用另一个值重新计算,这取决于具体情况。您应该小心使用
-replace
cmdlet替换html文件中的字符串。您应该尝试使用XML解析器。看:如果你已经搜索了一段时间,你一定尝试了一些东西。你有没有我们可以帮助你的例子。这是一个基本的请求,已经有很多相关的答案了。@Matt(Get Content test1.htm)| ForEach{$\u-replace',“”}| Set Content test2.htm但它不起作用,因为字符串是3行,比如:'\n\n',所以我只尝试了powershell。我得到了输出:方法调用失败,因为[System.Object[]不包含名为“replace”的方法。执行此命令时,没有结果[System.IO.File]::ReadAllText(“d:\test.xml”).replace('1234','xxxx')| sc d:\test.xml/*这只是一个示例*/
Get Content-Raw…
找不到与参数名称“Raw”匹配的参数。@lukesky
(Get Content“filename”| Out String)…
将起作用,因为您有PowerShell 2.0。不过,我认为您会遇到其他问题。@Matt这是我当前的代码:(获取内容“test1.htm”|输出字符串)。替换(“”,“”)|设置内容“test2.htm”,但它仍然无法工作。我得到输出:方法调用失败,因为[System.Object[]不包含名为“replace”的方法。执行此命令时,没有结果[System.IO.File]::ReadAllText(“d:\test.xml”).replace('1234','xxxx')| sc d:\test.xml/*这只是一个示例*/
Get Content-Raw…
找不到与参数名称“Raw”匹配的参数。@lukesky
(Get Content“filename”| Out String)…
将起作用,因为您有PowerShell 2.0。但我认为您会遇到其他问题。@Matt这是我当前的代码:(获取内容“test1.htm”|输出字符串)。替换(“”,“”)|设置内容“test2.htm”,但它仍然不起作用。