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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With