Powershell replace special characters like ü

浪子不回头ぞ 提交于 2021-01-28 03:41:15

问题


(Get-Content -Path $filePath) -creplace ${find}, $replace | Add-Content -Path $tempFilePath 

If $find and $replace contains below values it's not replacing it

ç c
à a
é e

Please help


回答1:


You need to -Encoding UTF8 to the Get-Content method, for reading the special characters correctly:

(Get-Content -Path $filePath -Encoding UTF8) -creplace ${find}, $replace | Add-Content -Path $tempFilePath 



回答2:


If by characters like ü you mean Diacritics you can use this:

function Replace-Diacritics {
    Param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string] $Text
    )
    ($Text.Normalize([Text.NormalizationForm]::FormD).ToCharArray() | 
     Where-Object {[Globalization.CharUnicodeInfo]::GetUnicodeCategory($_) -ne 
                   [Globalization.UnicodeCategory]::NonSpacingMark }) -join ''
}

# you can experiment with the `-Encoding` parameter
Get-Content -Path 'D:\Test\TheFile.txt' -Encoding UTF8 -Raw | Replace-Diacritics | Set-Content -Path 'D:\Test\TheNewFile.txt'


来源:https://stackoverflow.com/questions/62737875/powershell-replace-special-characters-like-%c3%bc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!