64

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. Commented Aug 14, 2023 at 16:23

6 Answers 6

52

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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"
Yeah, `n is \n, NOT \r\n.
33

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 Comment

thumbs up for that explanation
28

Use the -Raw parameter of Get-Content to match across multiple lines.

By default, Get-Content returns an array of lines from the file.

Example (from PowerShell 7) showing only adding the -Raw parameter returns what you expect:

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

Explanation quoted from Get-Help:

❯ Get-Help Get-Content -Parameter Raw

-Raw <System.Management.Automation.SwitchParameter>
    Ignores newline characters and returns the entire contents of a file in one string with the newlines
    preserved. By default, newline characters in a file are used as delimiters to separate the input into an
    array of strings. This parameter was introduced in PowerShell 3.0. Raw is a dynamic parameter that the
    FileSystem provider adds to the `Get-Content` cmdlet This parameter works only in file system drives.

4 Comments

Worth noting this isn't available with PS v2, but if you pipe to Out-String you'll get the same effect.
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
Can you please share example? I can't find it
Where should -Raw be used?
7

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.

Comments

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 Comment

wait, powershell with servicenow? is whats the use case?
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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.