locked
use powershell to find a string , and excludes some string RRS feed

  • Question

  • I have a large file , I would like to find all lines with the string "error" but excludes those the lines with the string "testing" .

    line 1 xxx error xxx
    line 2 xxx error xxx
    line 3 xxx error testing xxx
    line 4 xxx yyy

    In the above , what I expect the result is only display line 1 and line 2 , would advise what is the command line can do that ?
    I tried the command find /I "error" /V "testing my_file but not work .

    Thanks
    Tuesday, September 13, 2016 2:43 PM

Answers

  • Use Select-String:

    http://ss64.com/ps/select-string.html


    EDIT:

    Select-String -Path .\input.txt -Pattern 'error' -SimpleMatch |
        Where {$_.Line -notlike '*testing*'}


    • Edited by Mike Laughlin Tuesday, September 13, 2016 2:58 PM
    • Proposed as answer by Vincent Karunaidas Tuesday, September 13, 2016 3:21 PM
    • Marked as answer by Hello_2018 Tuesday, September 20, 2016 7:45 AM
    Tuesday, September 13, 2016 2:50 PM
  • select-string -path .\input.txt -Pattern '(?=.*error)(?!.*testing)'


    \_(ツ)_/

    • Proposed as answer by Mike Laughlin Tuesday, September 13, 2016 3:57 PM
    • Marked as answer by Hello_2018 Tuesday, September 20, 2016 7:45 AM
    Tuesday, September 13, 2016 3:51 PM

All replies

  • Use Select-String:

    http://ss64.com/ps/select-string.html


    EDIT:

    Select-String -Path .\input.txt -Pattern 'error' -SimpleMatch |
        Where {$_.Line -notlike '*testing*'}


    • Edited by Mike Laughlin Tuesday, September 13, 2016 2:58 PM
    • Proposed as answer by Vincent Karunaidas Tuesday, September 13, 2016 3:21 PM
    • Marked as answer by Hello_2018 Tuesday, September 20, 2016 7:45 AM
    Tuesday, September 13, 2016 2:50 PM
  • select-string -path .\input.txt -Pattern '(?=.*error)(?!.*testing)'


    \_(ツ)_/

    • Proposed as answer by Mike Laughlin Tuesday, September 13, 2016 3:57 PM
    • Marked as answer by Hello_2018 Tuesday, September 20, 2016 7:45 AM
    Tuesday, September 13, 2016 3:51 PM