使用 PowerShell 搜索特殊字符 U+FFFD (65533)
Search for special character U+FFFD (65533) with PowerShell
我要搜索 .txt 文件中出现的每个字符。它是“�”或更广为人知的 0xFFFD。
好吧,如果我正在搜索普通字符串,我可以让一切正常工作,但我无法使用这个特殊字符。
想象一下,我在 $line 引用的文本文件中有一个特定的行,我正在谈论的特殊字符就在其中,例如:
$line = 'you cann cho�se f�r everyone'
然后我尝试创建以下变量:
$SearchCharacter1 = "�"
$SearchCharacter2 = $([char]0xFFFD)
然后我使用 Select-String 命令在下面的 Powershell 代码中使用了它们
if($line | Select-String -Pattern $SearchCharacter -SimpleMatch)
{
Write-Host "Character involved"
}else
{
Write-Host "Character not involved"
}
我也试过这样一个简单的包含语句:
if($line.Contains($SearchCharacter))
{
Write-Host "Character involved"
}else
{
Write-Host "Character not involved"
}
有谁知道如何让代码正常工作吗? 0xFFFD 有什么特别之处?为什么当我使用普通字符时一切正常。
感谢您的宝贵时间!
编辑:
我得到这样的行:
foreach ($file in $files){
$lines = Get-Content $file
foreach ($line in $lines){
# Check if the line contains the character we need to change.
if($line -match $SearchCharacter)
{
# And now do sth with $line
你能试试这个吗
if($line -match $SearchCharacter) {
Write-Host "Character involved"
}
else {
Write-Host "Character not involved"
}
我怀疑文件编码为 utf8nobom,而您使用的是 powershell 5,它不会自动识别编码。为此,您必须指定编码:
get-content file -encoding utf8 | select-string �
you cann cho�se f�r everyone
UTF8 将 � 编码为 "EF BF BD":
format-hex file
Label: /Users/js/file
Offset Bytes Ascii
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
------ ----------------------------------------------- -----
0000000000000000 79 6F 75 20 63 61 6E 6E 20 63 68 6F EF BF BD 73 you cann cho�s
0000000000000010 65 20 66 EF BF BD 72 20 65 76 65 72 79 6F 6E 65 e f�r everyone
0000000000000020 0A �
我要搜索 .txt 文件中出现的每个字符。它是“�”或更广为人知的 0xFFFD。 好吧,如果我正在搜索普通字符串,我可以让一切正常工作,但我无法使用这个特殊字符。 想象一下,我在 $line 引用的文本文件中有一个特定的行,我正在谈论的特殊字符就在其中,例如:
$line = 'you cann cho�se f�r everyone'
然后我尝试创建以下变量:
$SearchCharacter1 = "�"
$SearchCharacter2 = $([char]0xFFFD)
然后我使用 Select-String 命令在下面的 Powershell 代码中使用了它们
if($line | Select-String -Pattern $SearchCharacter -SimpleMatch)
{
Write-Host "Character involved"
}else
{
Write-Host "Character not involved"
}
我也试过这样一个简单的包含语句:
if($line.Contains($SearchCharacter))
{
Write-Host "Character involved"
}else
{
Write-Host "Character not involved"
}
有谁知道如何让代码正常工作吗? 0xFFFD 有什么特别之处?为什么当我使用普通字符时一切正常。 感谢您的宝贵时间!
编辑: 我得到这样的行:
foreach ($file in $files){
$lines = Get-Content $file
foreach ($line in $lines){
# Check if the line contains the character we need to change.
if($line -match $SearchCharacter)
{
# And now do sth with $line
你能试试这个吗
if($line -match $SearchCharacter) {
Write-Host "Character involved"
}
else {
Write-Host "Character not involved"
}
我怀疑文件编码为 utf8nobom,而您使用的是 powershell 5,它不会自动识别编码。为此,您必须指定编码:
get-content file -encoding utf8 | select-string �
you cann cho�se f�r everyone
UTF8 将 � 编码为 "EF BF BD":
format-hex file
Label: /Users/js/file
Offset Bytes Ascii
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
------ ----------------------------------------------- -----
0000000000000000 79 6F 75 20 63 61 6E 6E 20 63 68 6F EF BF BD 73 you cann cho�s
0000000000000010 65 20 66 EF BF BD 72 20 65 76 65 72 79 6F 6E 65 e f�r everyone
0000000000000020 0A �