57

Given test.txt containing:

test
message

I want to end up with:

testing
a message

I think the following should work, but it doesn't:

Get-Content test.txt |% {$_-replace "t`r`n", "ting`r`na "}

How can I do a find and replace where what I'm finding contains CRLF?

1
  • For anyone reading this later, % is an alias for ForEach-Object. Aug 14 at 16:23

6 Answers 6

48

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.

2
  • 2
    This answer is totally misleading, at least at present time (don't know if sth changed in 14 years). `n actually represent one single character, line feed. It doesn't consist of both! The problem with OP code is that, as Peter Seale answered, GC gives you file content in an array of strings, in which every entry is a line. If you need to match multiline content, you need to join the array previously with -join "`r`n"
    – Teejay
    Oct 3, 2022 at 7:32
  • 1
    Yeah, `n is \n, NOT \r\n.
    – codaamok
    Dec 15, 2022 at 9:24
31

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 :)

1
26

With -Raw you should get what you expect

4
  • 3
    Worth noting this isn't available with PS v2, but if you pipe to Out-String you'll get the same effect.
    – Robin
    May 30, 2014 at 15:49
  • 4
    I guess the parameter is there as remedy for the inexcusable rudeness of the command in returning anything other than what is. +1 for this. I didn't know and I blew 1hr trying to figure out why my strings were missing their newlines
    – ekkis
    Aug 23, 2015 at 19:31
  • 2
    Can you please share example? I can't find it
    – Sagar S.
    May 5, 2021 at 8:09
  • Where should -Raw be used? Aug 14 at 16:28
6

If you want to remove all new line characters and replace them with some character (say comma) then you can use the following.

(Get-Content test.txt) -join ","

This works because Get-Content returns array of lines. You can see it as tokenize function available in many languages.

1

You can use "\\r\\n" also for the new line in powershell. I have used this in servicenow tool.

In my case "\r\n" s not working so i tried "\\r\\n" as "\" this symbol work as escape character in powershell.

1
  • wait, powershell with servicenow? is whats the use case? Mar 3 at 14:33
0

You can detect if a file is CRLF with this simple Powershell instruction

(cat -Raw $args) -match "\r\n$"

Replacing \n with \r\n is tricky because you have to detect it first and apply the replacement only if it is needed, otherwise it would be a mess. Too complex.

In any case, you can forget detection, to ensure a file is CRLF whatever the original type you can do this in PowerShell:

cat $file > $file

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.