简体   繁体   English

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

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

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? 如何使用PowerShell删除文本文件中行尾的多余空格?

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). 您可能需要调整Set-Content cmdlet上的Encoding参数,以获取所需编码(Unicode,ASCII,UTF8等)的文件输出。

For small files (less than 250 MB) you could use: 对于小文件(小于250 MB),您可以使用:

$file = "Log20130820"

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

For files that are too large, the script would fail with OutOfMemoryException. 对于太大的文件,脚本将失败,并显示OutOfMemoryException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM