Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I output lines that do not match 'this_string' using Get-Content and Select-String in PowerShell?

I found a post about users that wanted to use grep in PowerShell. For example,

PS> Get-Content file_to_grep | Select-String "the_thing_to_grep_for"

How do I output lines that are NOT this_string?

like image 803
chrips Avatar asked Nov 21 '11 16:11

chrips


3 Answers

Select-String has the NotMatch parameter.

get-content file_to_grep | select-string -notmatch "the_thing_to_grep_for"
like image 66
manojlds Avatar answered Oct 17 '22 07:10

manojlds


get-content file_to_grep | select-string "^(?!the_thing_to_grep_for$)"

will return the lines that are different from the_thing_to_grep_for.

get-content file_to_grep | select-string "^(?!.*the_thing_to_grep_for)"

will return the lines that don't contain the_thing_to_grep_for.

like image 30
Tim Pietzcker Avatar answered Oct 17 '22 07:10

Tim Pietzcker


gc  file_to_grep | ? {!$_.Contains("the_thing_to_grep_for")}

which is case-sensitive comparison by the way.

like image 36
vikas368 Avatar answered Oct 17 '22 07:10

vikas368