我知道我可以使用:
gc c:\FileWithEmptyLines.txt | where {$_ -ne ""} > c:\FileWithNoEmptyLines.txt
要删除空行,请执行以下操作。但是我怎么才能用'-replace‘删除它们呢?
发布于 2012-06-13 02:32:07
我在这里找到了一个很好的>> http://www.pixelchef.net/remove-empty-lines-file-powershell。我只测试了几个空行,包括只有换行符的空行,以及只有空格、制表符和组合的行。
(gc file.txt) | ? {$_.trim() -ne "" } | set-content file.txt
有关代码的一些注释,请参阅原文。很好:)
发布于 2012-02-10 21:58:41
如果您还想排除仅包含空格字符的文件,则可以使用-match代替-eq:
@(gc c:\FileWithEmptyLines.txt) -match '\S' | out-file c:\FileWithNoEmptyLines
发布于 2015-08-14 00:16:44
虽然不是专门使用-replace
,但是使用-notmatch
和正则表达式解析内容可以获得相同的效果。
(get-content 'c:\FileWithEmptyLines.txt') -notmatch '^\s*$' > c:\FileWithNoEmptyLines.txt
https://stackoverflow.com/questions/9223460
复制相似问题