Powershell: Find/Replace pattern of ASCII control characters

后端 未结 3 1042
长发绾君心
长发绾君心 2021-01-12 15:52

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 ch

3条回答
  •  生来不讨喜
    2021-01-12 16:24

    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
    }
    

提交回复
热议问题