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

前端 未结 3 636
野性不改
野性不改 2021-02-07 05:24

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\"
<         


        
相关标签:
3条回答
  • 2021-02-07 06:17
    gc  file_to_grep | ? {!$_.Contains("the_thing_to_grep_for")}
    

    which is case-sensitive comparison by the way.

    0 讨论(0)
  • 2021-02-07 06:21
    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.

    0 讨论(0)
  • 2021-02-07 06:23

    Select-String has the NotMatch parameter.

    get-content file_to_grep | select-string -notmatch "the_thing_to_grep_for"
    
    0 讨论(0)
提交回复
热议问题