Question:
I have large text files that contain invalid records.
I would like to remove all lines where the first field is blank. The file delimiter is a tilde , so essentially I want to remove all lines where the first character is ~
Can someone assist with the PowerShell code – I cannot seem to get it right.
Answer:
Just adding the answer part from my comment as an answer, so that people can see that the question has been answered.
The solution which worked from the person asking the question was:
1 2 |
Get-Content file.txt | Where { $_ -notmatch "^~" } | Set-Content filteredfile.txt |
As a note, be aware of potential encoding issues. If you need to specify encoding, both the Get-Content
and the Set-Content
methods have an -Encoding
parameter which you can use to specify encoding.