替换powershell文件中的行

问题描述 投票:0回答:1

我有以下情况。我有一个巨大的文本,里面有很多带有REPLACEMENT_CHARACTER". "的单词。我的脚本已经生成了一个字典,通过使用键值对来提供这些单词的正确翻译。它看起来是这样的。

"gew�hlte":  "gewählte"
"Betr�ge;":  "Beträge;"

我在这个字典里有大约1200个词条 在(巨大的)文本文件中,我在循环中使用这个命令来进行修正。

foreach($key in $solutionsDictionary.Keys)
{
    #Replace the key with value.
    [String]$value = $solutionsDictionary[$key]
    (Get-Content -encoding UTF8 $file) -replace [Regex]::Escape($key), "$value" | Set-Content -encoding UTF8 $file
}

但它的工作速度非常慢。为了加快速度,我想过滤真正包含这个字符的行,然后通过使用单词作为我的词典的键来修正这些行,而不是尝试每个键,直到我找到正确的。但是,我不知道如何才能在迭代内将单行写回文件,然后继续寻找下一行?新的不完全算法是这样的。

$SearchCharacter = '�'
$lines = get-content $file -encoding UTF8 | select-string $SearchCharacter
foreach ($line in $lines)
{
    # Split into words and find the ones which contain the searchCharacter
    $words = -split $line
    $words = @($words) -match $SearchCharacter

    foreach ($word in $words){
        # replace each word in the line.by using word as index.

        # Code missing here. How to write back a single line?
    }
}

如果 "select -string "属性是问题所在, 我可以不使用它进行替换.有什么建议可以告诉我怎么做吗? 非常感谢


Edit:The folllowing solution Came up:

$SearchCharacter = '�'
Get-Content $file -encoding UTF8 |
ForEach-Object {
    If ($_.Contains($SearchCharacter)) {
        $Words = $_ -Split '\s+'
        $words = @($words) -match $SearchCharacter
        ForEach ($Word in $Words) {
            If ($solutionsDictionary.ContainsKey($Word))
            {
                $_.Replace([Regex]::Escape($Word), $solutionsDictionary[$Word])
            }
        }
    }
    $_
} | Set-Content -encoding UTF8 $Outfile

到目前为止,它是可行的,但它有另一个缺点。目标文件每修改一个单词就会收到一行。我只是不知道如何防止这种情况发生。所以,比如说,对于那个输入,我得到的是这样的解决方案:

Das hier r�ckg�ngig ist das zu machen
r�ckg�ngig : ist bereits geamcht
Weitere W�rter gibt ers zu korrigieren
Hier noch ein bl�des Wort
zwei in einer Zeile G�hte und Gr��e

我得到了这个解决方案。

Das hier rückgängig ist das zu machen
Das hier r�ckg�ngig ist das zu machen
rückgängig : ist bereits geamcht
r�ckg�ngig : ist bereits geamcht
Weitere Wörter gibt ers zu korrigieren
Weitere W�rter gibt ers zu korrigieren
Hier noch ein blödes Wort
Hier noch ein bl�des Wort
zwei in einer Zeile Göhte und Gr��e
zwei in einer Zeile G�hte und Größe
zwei in einer Zeile G�hte und Gr��e

那么如何防止PowerShell为每一个修正字写一行新的内容呢?


编辑2:

正确的解决方案是插入$_=的赋值。

$SearchCharacter = '�'
Get-Content $file -encoding UTF8 |
ForEach-Object {
    If ($_.Contains($SearchCharacter)) {
        $Words = $_ -Split '\s+'
        $words = @($words) -match $SearchCharacter
        ForEach ($Word in $Words) {
            If ($solutionsDictionary.ContainsKey($Word))
            {
                $_ = $_.Replace([Regex]::Escape($Word), $solutionsDictionary[$Word])
            }
        }
    }
    $_
} | Set-Content -encoding UTF8 $Outfile
algorithm powershell parsing encoding text-files
1个回答
1
投票

我将使用你的第二个想法与PowerShell管道一起,为每一个 $Line 和a 散列表 来检查特殊词语。

$SearchCharacter = '�'
$ux4 = '\u{0:X4}' -f [bitconverter]::ToInt16([System.Text.Encoding]::Unicode.GetBytes($SearchCharacter))

$HashTable = ConvertFrom-StringData -Delimiter ':' '
gew�hlte: gewählte
Betr�ge: Beträge
'

Get-Content .\InFile.txt -encoding UTF8 |
ForEach-Object {
    If ($_ -Match "[\w$ux4]*$ux4+[\w$ux4]*") {
        ForEach ($Word in $Matches.Values) {
            If ($HashTable.ContainsKey($Word)) { $_ = $_.Replace($Word, $HashTable[$Word]) }
        }
    }
    $_
} | Set-Content -encoding UTF8 .\OutFile.txt

0
投票

我会使用内置的.NET方法。读取一次文件,使用以下方法循环检查键值 .Replace() 同时将输出分配回你的文件的shell副本。一旦你完成了每个键对的所有替换,就把它写回文件。我没有为你的字符串添加任何转义,但如果需要的话,你似乎已经有了这样做的方法。

$keys = # Your dictionary of words.
$file = [System.IO.File]::ReadAllText("./test.txt")
$keys.GetEnumerator() | Foreach-Object {
    $file = $file.Replace($_.Key,$keys[$_.Key], $true, [CultureInfo]::CurrentCulture)
}
[System.IO.File]::WriteAllText("./test.txt", $file)
© www.soinside.com 2019 - 2024. All rights reserved.