Replace the content of a textfile with a regex in powershell

后端 未结 2 1345
半阙折子戏
半阙折子戏 2021-02-03 17:29

I have a simple textfile and I need a powershell script to replace some parts of the file content.

My current script is the following:

$content = Get-Con         


        
2条回答
  •  渐次进展
    2021-02-03 18:04

    Yes, you can do that in one line and don't even need a pipeline, as -replace works on arrays like you would expect it to do (and you can chain the operator):

    (Get-Content Input.json) `
        -replace '"(\d+),(\d{1,})"', '$1.$2' `
        -replace 'second regex', 'second replacement' |
      Out-File output.json
    

    (Line breaks added for readability.)

    The parentheses around the Get-Content call are necessary to prevent the -replace operator being interpreted as an argument to Get-Content.

提交回复
热议问题