1

背景:我将 .mp4 视频的文件名更改为小写,并替换了特殊字符和空格。现在我必须以类似的方式更改 .txt 文件中的关联 URL。有许多文本文件包含大量引用视频的这些 URL。

问题:我应该替换任何文本文件中“flashplayer”和“/flashplayer”之间的每个字符串中的特殊字符,但不得更改flashplayer 标签之外的任何内容。

我不知道如何选择“flashplayer”和“/flashplayer”之间的字符串进行替换。

示例字符串:

(flashplayer width="640" height="480" position="1")file=/wiki/data/media/sales/a/ö 2.mp4&config=/wiki/lib/plugins/flashplayer/config_video.xml&start=0(/flashplayer)

此示例包含在文本文件(DokuWiki 页面)中。() 暗示标记字符。

示例输出字符串:

(flashplayer width="640" height="480" position="1")file=/wiki/data/media/sales/a/oe_2.mp4&config=/wiki/lib/plugins/flashplayer/config_video.xml&start=0(/flashplayer)

用 rename-item 替换应该是:

  • ä = ae
  • ö = oe
  • ü = ü
  • ' ' = '_'

更新:脚本看起来像:

# vars (User-Eingabe)
$source = "d:\here\name\test\pages"
$search = '(\<flashplayer.*?\>file\=/wiki/87sj38d/media)(.*?)(\<\/flashplayer\>)'
$a = 1
Write-Host "`nSource:`t $source`n"
# replace special characters
gci $source -r -Filter *.txt | ForEach-Object {
    $text = Get-Content $_.FullName | ForEach-Object {
        if($_ -match $search) {
            $_ -replace [Regex]::Escape($Matches[2]), ($Matches[2] -replace'ö', 'oe' -replace'ä', 'ae' -replace'ü', 'ue' -replace'\s', '_' )
            $output = $Matches[2]
            $tags = $a++         
            Write-Host "`nTag $tags : $output"
        } else {
            $_
        }
    }
    $text | Set-Content $_.FullName
}

文本文件包含一行代码,如下所示:

{{backlinks>path:product:description:kennwort_aendern}}

该脚本仅在我删除这行代码时才有效。否则 flashplayertags 之间的字符串保持不变。令人困惑的是,替换有时会起作用,有时不会。flashplayertags 之间的字符串可以包含许多特殊字符。请参阅示例字符串:

<flashplayer_width="640"_height="480"_position="1">file=/wiki/87sj38d/media/ab/any/test/1001_Grundlagen Kennwort ändern.mp4&image=/wiki/87sj38d/media/ab/any/test/1001_Grundlagen Kennwort ändern.jpg&config=/wiki/lib/plugins/flashplayer/config_video.xml&start=0</flashplayer>

Write-Host $output 正确显示所有字符串,但替换无法正常运行。

4

2 回答 2

2

你可以尝试这样的事情。对于每个文本文件,它将替换每一flashplayer行的特殊字符。

Get-ChildItem -Path "c:\FolderOfTextfiles" -Filter *.txt | ForEach-Object {

    $text = Get-Content $_.FullName | ForEach-Object {
        if($_ -match '(?<=\(flashplayer.*?\))(.*?)(?=\(/flashplayer\))') {
            $_ -replace [Regex]::Escape($Matches[1]), ($Matches[1] -replace'ö', 'oe' -replace 'ä', 'ae' -replace 'ü', 'ue' -replace '\s', '_' )
        } else {
            $_
        }
    }

    $text | Set-Content $_.FullName

}

更新:如果文本包含换行符,那么您可以尝试这个全局多行正则表达式匹配方法:

$s = @'
<flashplayer_width="640"_height="480"_position="1">file=/wiki/87sj38d/media/ab/any/test/1001_Grundlagen Kennwort ändern.mp4&image=/wiki/87sj38d/media/ab/
any/test/1001_Grundlagen Kennwort ändern.jpg&config=/wiki/lib/plugins/flashplayer/config_video.xml&start=0</flashplayer>
<flashplayer_width="640"_height="480"_position="1">file=/wiki/87sj38f/media/ab/any/test/1001_Grundlagen Kennwort ändern.mp4&image=/wiki/87sj38d/media/ab/any/test/1001_Grundlagen Kennwort ändern.jpg&
config=/wiki/lib/plugins/flashplayer/config_video.xml&start=0</flashplayer>
'@

#Read text as single string
#PS 3.0+
#$s = Get-Content .\test.txt -Raw

#PS 2.0
#$s = Get-Content .\test.txt | Out-String

$s = [regex]::Replace($s, '(?s)(?<=<flashplayer.*?>file=/wiki/87sj38d/media).*?(?=</flashplayer>)', { 
    param([System.Text.RegularExpressions.Match]$m)
    $m.Value -replace 'ö', 'oe' -replace 'ä', 'ae' -replace 'ü', 'ue' -replace ' ', '_'
})

$s    

#Save
#$s | Set-Content .\test.txt

这是一个更复杂的解决方案,因为 AFAIK在当前 PowerShell 版本中使用时无法修改$1(捕获的组) 。-replace 'pattern', '$1'如果有人有更好的解决方案,请分享:)

于 2014-07-23T13:10:05.390 回答
0

在这里,您可以使用可替换上述字符的命令。您需要根据文本文件的位置更改文件路径。Replace-FileString.ps1 被使用;http://windowsitpro.com/scripting/replacing-strings-files-using-powershell

./Replace-FileString  -Pattern '(flashplayer)(.*)ä(.*)(\/flashplayer)'  -Replacement '$1$2ae$3$4'  -Path C:\test\*.txt  -Overwrite
./Replace-FileString  -Pattern '(flashplayer)(.*)ö(.*)(\/flashplayer)'  -Replacement '$1$2oe$3$4'  -Path C:\test\*.txt  -Overwrite
./Replace-FileString  -Pattern '(flashplayer)(.*)ü(.*)(\/flashplayer)'  -Replacement '$1$2ue$3$4'  -Path C:\test\*.txt  -Overwrite
./Replace-FileString  -Pattern '(flashplayer)(.*) (.*)(\/flashplayer)'  -Replacement '$1$2_$3$4'  -Path C:\test\*.txt  -Overwrite

它打开并写入所有文本文件(即使它没有改变任何东西)。它只更改字符串“flashplayer”和“/flashplayer”之间的“ä”、“ö”、“ü”或“”行。

于 2014-07-23T13:07:35.907 回答