簡體   English   中英

如果Powershell中的行為空,則刪除文件中的行

[英]Delete a line in file if it is empty in powershell

如何刪除文件中的空行?

當我搜索它時,人們正在創建另一個文件來移動非空行,如下所示

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

有什么辦法可以刪除源文件本身中的行?

為什么不能將其保存回同一文件? 將其分為兩行:

$var = gc c:\PST\1.txt | Where {$_ -ne ""}
$var > c:\pst\1.txt

跳過與空格,制表符或換行符匹配的行:

(Get-Content c:\FileWithEmptyLines.txt) | `
  Where-Object {$_ -match '\S'} | `
   Out-File c:\FileWithEmptyLines.txt

更新:對於多個文件:

$file = Get-ChildItem -Path "E:\copyforbuild*xcopy.txt" 

foreach ($f in $file)
{ 
    (Get-Content $f.FullName) | `
      Where-Object {$_ -match '\S'} | `
       Out-File $f.FullName
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM