Reputation: 18157
How to delete a empty line in file?
When i searched for it , people are creating another file to move non empty lines like follows
gc c:\FileWithEmptyLines.txt | where {$_ -ne ""} > c:\FileWithNoEmptyLines.txt
Is there any way to delete the line in the source file itself?
Upvotes: 2
Views: 6640
Reputation: 126762
Skip lines that match a space, a tab or a line break:
(Get-Content c:\FileWithEmptyLines.txt) | `
Where-Object {$_ -match '\S'} | `
Out-File c:\FileWithEmptyLines.txt
UPDATE: for multiple files:
$file = Get-ChildItem -Path "E:\copyforbuild*xcopy.txt"
foreach ($f in $file)
{
(Get-Content $f.FullName) | `
Where-Object {$_ -match '\S'} | `
Out-File $f.FullName
}
Upvotes: 3
Reputation: 13483
Why can't you save it back to the same file? Split it into 2 lines:
$var = gc c:\PST\1.txt | Where {$_ -ne ""}
$var > c:\pst\1.txt
Upvotes: 4