How can I replace newlines using PowerShell?

前端 未结 5 2106
野的像风
野的像风 2020-12-08 18:24

Given test.txt containing:

test
message

I want to end up with:

testing
a message

I think the fol

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 19:22

    In my understanding, Get-Content eliminates ALL newlines/carriage returns when it rolls your text file through the pipeline. To do multiline regexes, you have to re-combine your string array into one giant string. I do something like:

    $text = [string]::Join("`n", (Get-Content test.txt))
    [regex]::Replace($text, "t`n", "ting`na ", "Singleline")
    

    Clarification: small files only folks! Please don't try this on your 40 GB log file :)

提交回复
热议问题