Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PowerShell to remove space from the end of a line in a text file

Tags:

powershell

I found a find-and-replace text answer from another question.

How do I use PowerShell to remove extra spaces at end of line in a text file?

like image 846
Swan Lee Avatar asked Aug 26 '10 20:08

Swan Lee


1 Answers

There are a number of approaches but this one is pretty simple:

$content = Get-Content file.txt
$content | Foreach {$_.TrimEnd()} | Set-Content file.txt

You may need to tweak the Encoding parameter on the Set-Content cmdlet to get the file output in the encoding you want (Unicode, ASCII, UTF8, etc).

like image 78
Keith Hill Avatar answered Oct 28 '22 01:10

Keith Hill