Powershell: Find/Replace pattern of ASCII control characters

我只是一个虾纸丫 提交于 2019-12-01 03:07:48

问题


I'm trying to write a script to search the contents of a file, and when it comes across a grouping of ASCII control characters, to insert a CR/LF.

The pattern of characters I would like to replace are [ETX][NUL][STX][ETX][SOH]

    $filenames = @(Get-Childitem "E:\VendorFiles\*")

    $CR = @("[char]3 [char]0 [char]2 [char]3 [char]1")
    foreach ($file in $filenames) {$outfile = "$file" + ".txt"
    Get-Content $file | Foreach-object {
            $_ -replace $CR,"`r`n" `
             -replace [char]3,"|" `
             -replace [char]1,"{" `
             -replace "\\","\\" `
        } | Set-Content -encoding "UTF8" $outfile}

回答1:


This expression:

@("[char]3 [char]0 [char]2 [char]3 [char]1")

...creates an array with a single element. You need commas between terms if you really want an array of 5 items, but -replace does not support arrays anyway. Also, your single element contains the literal characters you typed; not what you expected.

What you need is to create a simple string to feed to -replace; this is a bit more involved when you are dealing with non-printable characters. You had the right idea--you just have to tell PowerShell to interpolate the code expressions within your string using the $() notation on each expression:

$CR = "$([char]3)$([char]0)$([char]2)$([char]3)$([char]1)"



回答2:


Michael Sorens' helpful answer explains the problem with your approach well and offers a working solution.

To offer a simpler alternative:

$CR = ([char[]] (3, 0, 2, 3, 1)) -join ''
  • 3, 0, 2, 3, 1 creates an array of integers with the Unicode code points of the characters to create.

  • Cast [char[]] converts the code points to actual characters ([char]).

  • -join '' joins the array of characters (with no separator) to for a single string.




回答3:


I have a function in a script that does something like this. Not sure if this will help you:

# This function will make a new file that has custom comments
# it will comment out "rollback tran"
# it will uncomment out "--commit tran"
function CommentAndUncomment($TheScript)
{
    PrintTextAndTime("About to run this SQL file: $TheScript")
    PrintTextAndTime("Will comment out 'rollback tran' and uncomment '--commit tran'")
    $content = Get-Content $TheScript
    $content | 
      ForEach-Object { 
        if ($_ -clike "*ROLLBACK TRAN;*") { 
          $_ -replace "ROLLBACK TRAN;", "--ROLLBACK TRAN;"
        } 
        elseif ($_ -clike "*--COMMIT TRAN;*") { 
          $_ -replace "--COMMIT TRAN;", "COMMIT TRAN;"
        }
        else{
            $_
        }
      } | 
      Set-Content $ModifiedCommentsFile
    echo $ModifiedCommentsFile
    sqlcmd -i $ModifiedCommentsFile
    del $ModifiedCommentsFile
}


来源:https://stackoverflow.com/questions/15343442/powershell-find-replace-pattern-of-ascii-control-characters

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