locked
Equivalent to UNIX grep -w RRS feed

  • Question

  • When I select a string from a file it is showing several strings, ex

    $strings|select-string -pattern macro  will display

    macro

    gamacro

    tamacro

    I am looking equivalent command of grep -w which will only grep macro, is it possible?

    Thanks in advance

    Wednesday, June 6, 2018 12:15 AM

All replies

  • $strings|select-string -pattern '\bmacro\b'

    \_(ツ)_/

    Wednesday, June 6, 2018 12:18 AM
  • Thanks it is working but if it is not working if I have "-"

    macro

    gamacro

    tamacro

    ta-macro

    If I search $strings|select-string -pattern '\bmacro\b' it is searching

    macro 

    ta-macro

    Thanks in advance

    Wednesday, June 6, 2018 2:45 PM
  • "-" is not a word break character.  "-w" specifies only word breaks.


    \_(ツ)_/


    • Edited by jrv Wednesday, June 6, 2018 3:50 PM
    Wednesday, June 6, 2018 3:50 PM
  • This method seems to work.  Hopefully it helps.

    ($strings -notmatch "[^\s\\]macro") -notmatch "macro[^\s\\]"

     
    Friday, June 8, 2018 7:42 AM
  • $strings -replace '-','_' | select-string \bmacro\b

    • Edited by JS2010 Friday, June 8, 2018 3:28 PM
    Friday, June 8, 2018 2:53 PM