如何使用 PowerShell 从文本文件中的行尾删除空格

问题描述:

我从另一个问题中找到了一个查找和替换文本答案.

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

如何使用 PowerShell 删除文本文件行尾的多余空格?

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

有很多方法,但这个很简单:

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

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

您可能需要调整 Set-Content cmdlet 上的 Encoding 参数,以获得所需编码(Unicode、ASCII、UTF8 等)的文件输出.

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