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,032
  • 20
  • 99
  • 180
chrips
  • 4,506
  • 4
  • 21
  • 45

3 Answers3

56

Select-String has the NotMatch parameter.

get-content file_to_grep | select-string -notmatch "the_thing_to_grep_for"
manojlds
  • 275,671
  • 58
  • 453
  • 409
  • 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
  • 313,408
  • 56
  • 485
  • 544
  • 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
4
gc  file_to_grep | ? {!$_.Contains("the_thing_to_grep_for")}

which is case-sensitive comparison by the way.

vikas368
  • 1,328
  • 1
  • 8
  • 13