remove empty lines from text file with PowerShell

后端 未结 11 1820
情话喂你
情话喂你 2020-12-08 19:32

I know that I can use:

gc c:\\FileWithEmptyLines.txt | where {$_ -ne \"\"} > c:\\FileWithNoEmptyLines.txt

to remove empty lines. But How

11条回答
  •  悲哀的现实
    2020-12-08 19:59

    I found a nice one liner here >> http://www.pixelchef.net/remove-empty-lines-file-powershell. Just tested it out with several blanks lines including newlines only as well as lines with just spaces, just tabs, and combinations.

    (gc file.txt) | ? {$_.trim() -ne "" } | set-content file.txt
    

    See the original for some notes about the code. Nice :)

提交回复
热议问题