remove empty lines from text file with PowerShell

后端 未结 11 1828
情话喂你
情话喂你 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 20:04

    To resolve this with RegEx, you need to use the multiline flag (?m):

    ((Get-Content file.txt -Raw) -replace "(?m)^\s*`r`n",'').trim() | Set-Content file.txt
    

提交回复
热议问题