gpt4 book ai didi

powershell - Cmd 到 powershell 替换 - 特殊字符

转载 作者:行者123 更新时间:2023-12-02 23:57:23 28 4
gpt4 key购买 nike

我正在创建一个脚本,它将复制一个文件,重命名它,然后查看内部以删除某些特殊字符。其中一个特殊字符是某种 ASCII 撇号,我无法用键复制。我可以复制和粘贴它,但是替换功能不起作用。

打开文件 > 搜索奇怪的撇号 ' 并替换为空。我希望它用普通的撇号替换它,但我不知道这是如何完成的,目前最大的问题是我无法让它“看到”这个奇怪的撇号,它会自动生成我正在修改的文件。非常感谢任何帮助。谢谢 :)

文件中的撇号:'

普通撇号:'

这是我隔离测试的批处理的一部分。

        @echo off

set YYMMDD=%DATE:~-2,2%%DATE:~-7,2%%DATE:~-10,2%
set DDMMYYYY=%DATE:~-10,2%%DATE:~-7,2%%DATE:~-4,4%
set YYYY-MM-DD=%DATE:~-4,4%-%DATE:~-7,2%-%DATE:~-10,2%

powershell -Command "(gc 'C:\LOCATION\Client_List_%DDMMYYYY%.csv') -replace '’', '' | Out-File 'C:\LOCATION\Client_List_%DDMMYYYY%.csv'"

Echo Done

最佳答案

set "fileIn=C:\LOCATION\Client_List_%DDMMYYYY%.csv"
set "fileOu=C:\LOCATION\Client_List_%DDMMYYYY%.csv"
powershell -c "(gc '%fileIn%').Replace('‘‘','').Replace('’’','')|Out-File '%fileOu%'"

那个奇怪的撇号 U+2019右单引号,应该是结束引号。它可以搭配不同的开场白。在上面的例子中, U+2018左单引号。

Get-Help 'about_Quoting_Rules'

Quotation marks are used to specify a literal string. You can enclose a string in single quotation marks (') or double quotation marks (").



事实上,PowerShell 接受两个不同的 报价单:
  • 双引号 "
  • 单引号 '

  • AFAIK,所有这些引号都存在于大多数 Windows ANSI 代码页(1252、1250、1257、1253、1251、1254、1255、1256、1258)中,因此它们可以按字面意思在 ANSI 中使用-保存 .bat脚本 - 除了后一个引号 U+201B单高反9引号 .在这种情况下,请使用 $([char]0x201B)而不是 '‛‛'如下:
    rem        cast [char] to `[string]`    ↓↓↓↓↓↓↓↓
    powershell -c "(gc '%fileIn%').Replace( [string]$([char]0x201B) , '')"
    rem ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

    或如下:
    rem [char] can't be empty so specify `[string]`           ↓↓↓↓↓↓↓↓
    powershell -c "(gc '%fileIn%').Replace( $([char]0x201B) , [string]'')"
    rem ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

    分析说明

    下一个 PowerShell 代码片段显示了 Unicode 数据库的摘录(字符名称以 Quotation Mark 结尾或包含 Apostrophe ):
    PS D:> 0x22,0x27,0x00AB,0x00BB,0x2018,0x2019,0x201A,0x201B,0x201C,0x201D,0x201E,0x201F,
    0x2039,0x203A,0x2E42,0x301D,0x301E,0x301F,0x055A | Get-CharInfo | Format-Table -AutoSize

    Char CodePoint Category Description
    ---- --------- -------- -----------
    " U+0022 OtherPunctuation Quotation Mark
    ' U+0027 OtherPunctuation Apostrophe
    « U+00AB InitialQuotePunctuation Left-Pointing Double Angle Quotation Mark
    » U+00BB FinalQuotePunctuation Right-Pointing Double Angle Quotation Mark
    ‘ U+2018 InitialQuotePunctuation Left Single Quotation Mark
    ’ U+2019 FinalQuotePunctuation Right Single Quotation Mark
    ‚ U+201A OpenPunctuation Single Low-9 Quotation Mark
    ‛ U+201B InitialQuotePunctuation Single High-Reversed-9 Quotation Mark
    “ U+201C InitialQuotePunctuation Left Double Quotation Mark
    ” U+201D FinalQuotePunctuation Right Double Quotation Mark
    „ U+201E OpenPunctuation Double Low-9 Quotation Mark
    ‟ U+201F InitialQuotePunctuation Double High-Reversed-9 Quotation Mark
    ‹ U+2039 InitialQuotePunctuation Single Left-Pointing Angle Quotation Mark
    › U+203A FinalQuotePunctuation Single Right-Pointing Angle Quotation Mark
    ⹂ U+2E42 OtherNotAssigned Undefined
    〝 U+301D OpenPunctuation Reversed Double Prime Quotation Mark
    〞 U+301E ClosePunctuation Double Prime Quotation Mark
    〟 U+301F ClosePunctuation Low Double Prime Quotation Mark
    ՚ U+055A OtherPunctuation Armenian Apostrophe

    (修改后的 Get-CharInfo cmdlet 的输出。)原始 Get-CharInfo模块可从 http://poshcode.org/5234 下载.

    下一个 PowerShell 脚本通过显示一些有效(在我的语言环境中无效)引号组合来完成上述结果:

    $arrSingleQuotes = 
    ''' U+0027 Apostrophe ''' ,
    ‘‘‘ U+2018 Left Single Quotation Mark ‘‘‘ ,
    ’’’ U+2019 Right Single Quotation Mark ’’’ ,
    ‚‚‚ U+201A Single Low-9 Quotation Mark ‚‚‚ ,
    ‛‛‛ U+201B Single High-Reversed-9 Quotation Mark ‛‛‛ ,
    ‘‘‘ U+2018 (Left/Right) Single Quotation Mark U+2019 ’’’ ,
    ’’’ U+2019 (Right/Left) Single Quotation Mark U+2018 ‘‘‘
    '$arrSingleQuotes (any combination)'
    $arrSingleQuotes

    $arrDoubleQoutes =
    """ U+0022 Quotation Mark """ ,
    “““ U+201C Left Double Quotation Mark “““ ,
    ””” U+201D Right Double Quotation Mark ””” ,
    „„„ U+201E Double Low-9 Quotation Mark „„„ ,
    “““ U+201C (Left/Right) Double Quotation Mark U+201D ””” ,
    ””” U+201D (Right/Left) Double Quotation Mark U+201C “““
    '$arrDoubleQoutes (any combination)'
    $arrDoubleQoutes

    $noQuotes = @"
    « U+00AB Left-Pointing Double Angle Quotation Mark
    » U+00BB Right-Pointing Double Angle Quotation Mark
    ‟ U+201F Double High-Reversed-9 Quotation Mark
    ⹂ U+2E42 DOUBLE LOW-REVERSED-9 QUOTATION MARK
    ‹ U+2039 Single Left-Pointing Angle Quotation Mark
    › U+203A Single Right-Pointing Angle Quotation Mark
    〝 U+301D Reversed Double Prime Quotation Mark
    〞U+301E Double Prime Quotation Mark
    〟U+301F Low Double Prime Quotation Mark
    ՚ U+055A Armenian Apostrophe
    "@
    '$noQuotes'
    $noQuotes

    输出 :

    PS D:> D:\PShell\SO\41488245_quotes.ps1

    $arrSingleQuotes (any combination)
    ' U+0027 Apostrophe '
    ‘ U+2018 Left Single Quotation Mark ‘
    ’ U+2019 Right Single Quotation Mark ’
    ‚ U+201A Single Low-9 Quotation Mark ‚
    ‛ U+201B Single High-Reversed-9 Quotation Mark ‛
    ‘ U+2018 (Left/Right) Single Quotation Mark U+2019 ’
    ’ U+2019 (Right/Left) Single Quotation Mark U+2018 ‘

    $arrDoubleQoutes (any combination)
    " U+0022 Quotation Mark "
    “ U+201C Left Double Quotation Mark “
    ” U+201D Right Double Quotation Mark ”
    „ U+201E Double Low-9 Quotation Mark „
    “ U+201C (Left/Right) Double Quotation Mark U+201D ”
    ” U+201D (Right/Left) Double Quotation Mark U+201C “

    $noQuotes
    « U+00AB Left-Pointing Double Angle Quotation Mark
    » U+00BB Right-Pointing Double Angle Quotation Mark
    ‟ U+201F Double High-Reversed-9 Quotation Mark
    ⹂ U+2E42 DOUBLE LOW-REVERSED-9 QUOTATION MARK
    ‹ U+2039 Single Left-Pointing Angle Quotation Mark
    › U+203A Single Right-Pointing Angle Quotation Mark
    〝 U+301D Reversed Double Prime Quotation Mark
    〞U+301E Double Prime Quotation Mark
    〟U+301F Low Double Prime Quotation Mark
    ՚ U+055A Armenian Apostrophe

    请注意 ⹂ U+2E42 DOUBLE LOW-REVERSED-9 QUOTATION MARK存在于 Unicode 数据库中,并在 PowerShell ISE 中正确呈现。

    附录 : 我找到了更多 的候选人引号 (仅显示从 Excerpt_From_UnicodeDataTxt.ps1 脚本获得的结果):
    PS > $x = .\tests\Excerpt_From_UnicodeDataTxt.ps1 -SearchString "Quotation|Apostrophe" | 
    Where-Object {$_.Category -match 'Punctuation'}

    PS > $x.Count
    23

    PS > $x

    Char CodePoint Category Description
    ---- --------- -------- -----------
    " U+0022 Po-OtherPunctuation Quotation Mark
    ' U+0027 Po-OtherPunctuation Apostrophe
    « U+00AB Pi-InitialQuotePunctuation Left-Pointing Double Angle Quotation Mark
    » U+00BB Pf-FinalQuotePunctuation Right-Pointing Double Angle Quotation Mark
    ՚ U+055A Po-OtherPunctuation Armenian Apostrophe
    ‘ U+2018 Pi-InitialQuotePunctuation Left Single Quotation Mark
    ’ U+2019 Pf-FinalQuotePunctuation Right Single Quotation Mark
    ‚ U+201A Ps-OpenPunctuation Single Low-9 Quotation Mark
    ‛ U+201B Pi-InitialQuotePunctuation Single High-Reversed-9 Quotation Mark
    “ U+201C Pi-InitialQuotePunctuation Left Double Quotation Mark
    ” U+201D Pf-FinalQuotePunctuation Right Double Quotation Mark
    „ U+201E Ps-OpenPunctuation Double Low-9 Quotation Mark
    ‟ U+201F Pi-InitialQuotePunctuation Double High-Reversed-9 Quotation Mark
    ‹ U+2039 Pi-InitialQuotePunctuation Single Left-Pointing Angle Quotation Mark
    › U+203A Pf-FinalQuotePunctuation Single Right-Pointing Angle Quotation Mark
    ❮ U+276E Ps-OpenPunctuation Heavy Left-Pointing Angle Quotation Mark Ornament
    ❯ U+276F Pe-ClosePunctuation Heavy Right-Pointing Angle Quotation Mark Ornament
    ⹂ U+2E42 Ps-OpenPunctuation Undefined
    〝 U+301D Ps-OpenPunctuation Reversed Double Prime Quotation Mark
    〞 U+301E Pe-ClosePunctuation Double Prime Quotation Mark
    〟 U+301F Pe-ClosePunctuation Low Double Prime Quotation Mark
    " U+FF02 Po-OtherPunctuation Fullwidth Quotation Mark
    ' U+FF07 Po-OtherPunctuation Fullwidth Apostrophe

    关于powershell - Cmd 到 powershell 替换 - 特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41488245/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com