locked
Search for specific string in text file RRS feed

  • Question

  • I am trying to search for a particular string in a log file. The string includes pipeline with it like "|500".

    Get-Content -Path "F:\BlueBox1.txt" | Select-String -Pattern "|500" 

    I have tried the above command but using pipeline I think is taken as 'or' in it. I have tried using (`) for escape but didn't work. Kindly suggest.


    Rahul Goyal

    Sunday, July 15, 2018 12:49 PM

Answers

  • The escape for RegEx is "\".

    Select-String-Path F:\BlueBox1.txt -Pattern '\|500'

    Use single quotes on patterns to avoid conflict with the PwoerShell command line.

    To escape any string:

    [regex]::Escape('1(234)?[1..5]   Get me! Out)(;')


    \_(ツ)_/

    • Marked as answer by RahulGoyal03 Sunday, July 15, 2018 1:38 PM
    Sunday, July 15, 2018 1:01 PM

All replies

  • The escape for RegEx is "\".

    Select-String-Path F:\BlueBox1.txt -Pattern '\|500'

    Use single quotes on patterns to avoid conflict with the PwoerShell command line.

    To escape any string:

    [regex]::Escape('1(234)?[1..5]   Get me! Out)(;')


    \_(ツ)_/

    • Marked as answer by RahulGoyal03 Sunday, July 15, 2018 1:38 PM
    Sunday, July 15, 2018 1:01 PM
  • Or use the -simplematch option to ignore any regex characters like '|':

    Get-Content -Path "F:\BlueBox1.txt" | Select-String -Pattern "|500" -SimpleMatch


    • Edited by JS2010 Sunday, July 15, 2018 4:12 PM
    Sunday, July 15, 2018 3:10 PM