How can I replace newlines using PowerShell?

前端 未结 5 2105
野的像风
野的像风 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:03

    A CRLF is two characters, of course, the CR and the LF. However, `n consists of both. For example:

    PS C:\> $x = "Hello
    >> World"
    
    PS C:\> $x
    Hello
    World
    PS C:\> $x.contains("`n")
    True
    PS C:\> $x.contains("`r")
    False
    PS C:\> $x.replace("o`nW","o There`nThe W")
    Hello There
    The World
    PS C:\>
    

    I think you're running into problems with the `r. I was able to remove the `r from your example, use only `n, and it worked. Of course, I don't know exactly how you generated the original string so I don't know what's in there.

提交回复
热议问题