1

Environment: Windows 10 pro 20H2, PowerShell 5.1.19041.1237

In a .txt file, my following PowerShell code is not replacing the newline character(s) with " ". Question: What I may be missing here, and how can we make it work?

C:\MyFolder\Test.txt File:

This is first line.
This is second line.
This is third line.
This is fourth line.

Desired output [after replacing the newline characters with " " character]:

This is first line. This is second line. This is third line. This is fourth line.

PowerShell code:

PS C:\MyFolder\test.txt> $content = get-content "Test.txt"
PS C:\MyFolder\test.txt> $content = $content.replace("`r`n", " ")
PS C:\MyFolder\test.txt> $content | out-file "Test.txt"

Remarks

The above code works fine if I replace some other character(s) in file. For example, if I change the second line of the above code with $content = $content.replace("third", "3rd"), the code successfully replaces third with 3rd in the above file.

nam
  • 21,967
  • 37
  • 158
  • 332
  • Does this answer your question? [Multiline regex to match config block](https://stackoverflow.com/questions/12572164/multiline-regex-to-match-config-block). [Classic PowerShell gotcha #8](https://stackoverflow.com/a/69644807/1701026) – iRon Oct 23 '21 at 19:08
  • Note that out-file uses utf16 encoding by default. – js2010 Oct 23 '21 at 20:10

2 Answers2

2

You need to pass -Raw parameter to Get-Content. By default, without the Raw parameter, content is returned as an array of newline-delimited strings.

Get-Content "Test.txt" -Raw

Quoting from the documentation,

-Raw

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.

Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
  • 1
    Your suggestion worked. Thank you for providing a reference - as it will help some other readers of this post, as well. – nam Oct 23 '21 at 17:41
1

The simplest way of doing this is to not use the -Raw switch and then do a replacement on it, but make use of the fact that Get-Content splits the content on Newlines for you.

All it then takes is to join the array with a space character.

(Get-Content -Path "Test.txt") -join ' ' | Set-Content -Path "Test.txt"

As for what you have tried:

By using Get-Content without the -Raw switch, the cmdlet returns a string array of lines split on the Newlines.
That means there are no Newlines in the resulting strings anymore to replace and all that is needed is to 'stitch' the lines together with a space character.

If you do use the -Raw switch, the cmdlet returns a single, multiline string including the Newlines.
In your case, you then need to do the splitting or replacing yourself and for that, don't use the string method .Replace, but the regex operator -split or -replace with a search string '\r?\n'.

The question mark in there makes sure you split on newlines in Windows format (CRLF), but also works on *nix format (LF).

Theo
  • 57,719
  • 8
  • 24
  • 41
  • Thank you for sharing your thoughts. My upvote. You probably also meant to pipe the output back to the file. Based on your suggestion, following worked for me, as well: `(Get-Content -Path "Test.txt") -join ' ' | Out-File "Test.txt"` – nam Oct 23 '21 at 20:26
  • @nam Ah yes, saving to file using the pipe I didn't mention, but perhaps better use `Set-Content` for that as @js2010 [commented](https://stackoverflow.com/questions/69690420/powershell-not-removing-new-line-characters/69691546?noredirect=1#comment123185290_69690420) – Theo Oct 23 '21 at 20:38