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

30,406

Solution 1

Select-String has the NotMatch parameter.

get-content file_to_grep | select-string -notmatch "the_thing_to_grep_for"

Solution 2

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.

Solution 3

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

which is case-sensitive comparison by the way.

Share:
30,406
chrips
Author by

chrips

Updated on November 23, 2020

Comments

  • chrips
    chrips over 3 years

    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?

  • rob
    rob almost 7 years
    you can of course do it in a single command select-string -path file_to_grep -notmatch "the_thing_to_grep_for"
  • chrips
    chrips over 4 years
    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
  • Tim Pietzcker
    Tim Pietzcker over 4 years
    @Chrips: hey, no need to apologize. Manojlds‘ answer is definitely better.