4

I have text files that are generated with 2 empty lines between each block of text. I could use Notepad++ to do this using replace \r\n\r\n with \r\n, but there has to be a way to do this automatically.

I've tried to come up with something in Powershell, but nothing has worked so far.

This is what I've tried so far:

(Get-Content .\test.txt).Replace("\n\n",'\n') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("\s+\r\n+",'\r\n') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("\r\n+",'') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("\n+",'') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("\r\n\r\n",'\r\n') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("^(\s+\r\n)",'\r\n') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("^(\s+\r\n+)",'\r\n') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("^(\r\n+)",'\r\n') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("\r\n",'\b') | Set-Content .\test.txt
4
  • What are the contents of Test.txt You might be able to remove the white spaces using the Trim() method. Commented Jun 24, 2016 at 23:27
  • 1
    Use ` instead of \ to escape control characters Commented Jun 24, 2016 at 23:37
  • @Ramil If you found any of the answers helpful, please accept them. Commented Jun 25, 2016 at 8:02
  • Sorry, forgot to add the contents.. I'm currently working on a test file, that looks something like that. text <empty line1><empty line2>next block of text (sorry, I don't know how to add empty lines in comments, but you should get what I mean :P) Commented Jun 25, 2016 at 8:31

2 Answers 2

6

Get-Content returns a list of strings, and not a whole piece of text, like you need. Ovbiously you meant running this Replace method on a string, and not on a list of strings.

Use Get-Content -Raw .\test.txt to load the file content as one long string.

Also, the correct form of the replacement would be:

Replace("`r`n`r`n", "`r`n")

To sum up:

(Get-Content -Raw .\test.txt).Replace("`r`n`r`n", "`r`n") | Set-Content .\test.txt

Will do the job.

Another approach would be to just filter out empty lines:

$data = Get-Content .\test.txt
$data | Where-Object { $_ } | Set-Content .\test.txt
Sign up to request clarification or add additional context in comments.

2 Comments

Another approach would be to just filter out empty lines: - $data -ne ''
@iTayb Thanks, it works :D I used the first solution since I don't want to remove all empty lines. So Powershell sometimes accepts regex.. commands as such \n, but sometimes requires the backtick version `n. Can you point me to some material why is that ?
4

PowerShell uses backticks ` not backslashes \ to escape special characters:

(Get-Content .\test.txt) -replace "(`r?`n){2}",$([Environment]::Newline) | Set-Content .\test.txt

Using the regex -replace operator with a conditional carriage return will match any type of newline.

1 Comment

Thanks for pointing this out. I found it out yesterday when I tried to escape _ (underscore) between a variable and a string (like: Rename-Item -Path $LogPath -NewName "$Today`_logfile.log"). Useful for anyone who haven't figured this out yet :)

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.