Using Powershell to delete lines from a text file based on certain keywords

In continuation with the previous post, there was an issue that was encountered in a certain text file. There was a lot of redundant data in the output file. Let’s say like this :

“Error 11111 : The check finished without errors”

“Error 2222 : Login succeeded for user AMAM”

We needed to eliminate lines from the output file that had these lines. So how do you do that?

You can add this line to your file :

Get-Content $OutputFile | Where-Object {$_ -notmatch ‘finished without errors’ -And $_ -notmatch ‘Login succeeded for user’ } | Set-Content Output_2.txt

So as you can see, the idea is to use the $_ -notmatch operator followed by your string. To combine multiple phrases, use the -And operator.

The only issue that I faced was that I had to output to a separate text file called Output_2.txt. The command did not work in my parent text output file.

Thx to this site : More_Comparitors_-NotMatch_also_-And_

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s