2013-12-19 62 views
4

我们有一个包含3000多个正在迁移到SharePoint站点的HTML文件的目录,我们需要清理一些数据。在Powershell中替换多个字符串的正确语法

具体情况:

  • 文件约1/3的包括XML头<?xml version="1.0" encoding="utf-8"?> SharePoint可并不喜欢。我们计划只删除该标题行。
  • 每个文件都有指向两个备用相关主页链接foo1.htmfoo.htm的“HOME”的javascript参数。我们想要将两者都改为绝对链接http:\\sharepoint.site\home.aspx
  • 每个文件还有一个javascript链接参数“显示”,我们只是想通过将其更改为''来隐藏它。

这里是我的功能至今:

function scrubXMLHeader { 
    $srcfiles  = Get-ChildItem $backupGuidePath -filter "*htm.*"        
    $srcfilecount = (Get-ChildItem $backupGuidePath).Count          
    $selfilecount = $srcfiles.Count                
    # Input and Ouput Path variables 
    $sourcePath  = $backupGuidePath 
    $destinationPath = $workScrubPath 
    "Input From: $($sourcePath)" | Log $messagLog -echo   
    " Output To: $($destinationPath)" | Log $messageLog -echo 
    # 
    $temp01 = Get-ChildItem $sourcePath -filter "*.htm" 
    foreach($file in $temp01) 
    { 
     $outfile = $destinationPath + $file 
     $content = Get-Content $file.Fullname | ? {$_ -notmatch "<\?xml[^>]+>" } 
     Set-Content -path $outfile -Force -Value $content 
    } 
} 

我想下面的两个编辑添加到每个文件:

-replace '("foo.htm", "", ">", "Home", "foo1.htm")', '("http:\\sharepoint.site\home.aspx", "", ">", "Home", "http:\\sharepoint.site\home.aspx") 
-replace 'addButton("show",BTN_TEXT,"Show","","","","",0,0,"","","");', '' 

我不知道如何将这些合并成一个单一语句,所以我打开文件,执行更改,保存并关闭文件,而不是三个单独的打开编辑 - 保存/关闭事务。我也不确定,用所有的引号和逗号,逃避这些角色的最好方法,或者围绕整个字符串的单引号是否足够。

理解到“asking regexes to parse arbitrary HTML is like asking Paris Hilton to write an operating system, it's sometimes appropriate to parse a limited, known set of HTML”,但在我的工具包,PowerShell的限制,我想了解两个-replace行添加到现有的$content变量......在大括号内用逗号分隔的最好方法?互相传送?

下面是这些最佳策略吗?还是有更好的?

$content = Get-Content $file.Fullname | ? {$_ -notmatch "<\?xml[^>]+>", 
    -replace '("foo.htm", "", ">", "Home", "foo1.htm")', '("http:\\sharepoint.site\home.aspx", "", ">", "Home", "http:\\sharepoint.site\home.aspx"), 
    -replace 'addButton("show",BTN_TEXT,"Show","","","","",0,0,"","","");', '' } 
+1

每次[解析HTML使用正则表达式(http://stackoverflow.com/a/1732454/1630171),一名女生死于某处。 [适当的工具](http://stackoverflow.com/a/20644942/1630171)在您的处置。使用它们。 –

+0

保存CATGIRLS!不幸的是,我的工具集扩展到PowerShell v1.0;我处于用户区域,并且锁定了比我想要做的更多的工作。我敢肯定,如果IT能够找到一种方法从Win7中解脱PS来阻止我使用它,他们会。没有提到的适当工具可以提供给我,因为我没有适当的权限......不要让我开始。 – dwwilson66

+0

'Tidy'是用于美化代码的可选项。其余内置于Windows/PowerShell中。 –

回答

2

如果我正确读取的问题,我觉得这可能会做你想要什么:

$Regex0 = '<?xml version="1.0" encoding="utf-8"?> ' 

$Regex1 = '("foo.htm", "", ">", "Home", "foo1.htm")' 
$Replace1 = '("http:\\sharepoint.site\home.aspx", "", ">", "Home", "http:\\sharepoint.site\home.aspx")' 

$Regex2 = 'addButton("show",BTN_TEXT,"Show","","","","",0,0,"","","");' 


foreach($file in $temp01) 
    { 
     $outfile = $destinationPath + $file 
     (Get-Content $file.Fullname) -notmatch $Regex0,'' -replace $Regex1,$Replace1 -replace $Regex2,'' | 
     Set-Content -path $outfile -Force -Value $content 
    } 
+0

理论上,是的,但没有正则表达式。 :)使用正则表达式的行对我来说很优雅,因为它只是排除匹配模式的行;我试图弄清楚如何在其中添加另外两个'-replace'行......可以在花括号中包含一系列的staements并用逗号分隔吗?每次传递的结果传递给下一个'-replace'? – dwwilson66

+0

更新了脚本。您可以链接匹配/不匹配和替换运算符,并将筛选/替换结果传递给下一个运算符,因此您不需要中间的管道。 – mjolinor

+0

啊...这是有道理的。谢谢。我还更新了我的问题,以使具体更清楚,并且明显表明我没有试图用正则表达式来杀死猫女。 :) – dwwilson66

相关问题