21

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?

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
chrips
  • 4,996
  • 5
  • 24
  • 48

3 Answers3

56

Select-String has the NotMatch parameter.

get-content file_to_grep | select-string -notmatch "the_thing_to_grep_for"
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • you can of course do it in a single command `select-string -path file_to_grep -notmatch "the_thing_to_grep_for"` – rob Jun 14 '17 at 10:06
14
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.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Sorry, I decided to move the correct/accepted answer to the select-string -notmatch version below. I personally would rather use your regexy version but for the grand majority, I thought that I should move the accepted answer to fit the nature of this platform – chrips Oct 02 '19 at 17:51
  • @Chrips: hey, no need to apologize. Manojlds‘ answer is definitely better. – Tim Pietzcker Oct 02 '19 at 19:54
5
gc  file_to_grep | ? {!$_.Contains("the_thing_to_grep_for")}

which is case-sensitive comparison by the way.

vikas368
  • 1,408
  • 2
  • 10
  • 13