Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove lines from text file if it starts with

Tags:

powershell

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.

like image 455
user2725402 Avatar asked Dec 08 '22 10:12

user2725402


1 Answers

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.

like image 170
Robert Westerlund Avatar answered Dec 16 '22 13:12

Robert Westerlund