0

I'm using something like

(Get-Content $_.FullName | SomefilterHere ) | ? {$_.Trim() -ne "" } | Set-Content $_.FullName

to process files.

It applies some Regex filter (details not relevant here), then removes empty lines, any finally writes out the file again.

Now thing is, the input files have some text on their very last line without a final newline (CRLF as it's on Windows).

But the result from above will generate a newline on the last line. I don't want to have that. How can i avoid it?

Note this is not about newlines on every line, but only on the very last line of the output file. It seems Set-Content generates one by default.

Scrontch
  • 3,275
  • 5
  • 30
  • 45

1 Answers1

2

You could convert your file contents into a string using Out-String. Then just do a replace on the final CRLF (\r\n$). Finally, run your Set-Content with the -NoNewLine parameter.

((Get-Content $_.FullName | SomefilterHere ) | ? {
   $_.Trim() -ne "" } | Out-String) -Replace "\r\n$" | 
   Set-Content $_.FullName -NoNewLine
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27