Home > Net >  PowerShell not removing new line characters
PowerShell not removing new line characters

Time:10-27

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.

CodePudding user response:

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.

CodePudding user response:

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

  • Related