Announcement

Collapse
No announcement yet.

Search file content with special char

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Search file content with special char

    Hello Team,

    For the life of me, I can't figure this out. I need help.

    I want to search the string in my text file that contained a "$" string.. for example.. "myas$"
    The problem is "$" is the same a powershell special char. so the search never recognized "$" as a string.

    here is a sample code:
    Code:
    get-content "c:\myfile.txt" | `
    Where {$_ -match "*$"} | `
    Set-Content "c:\myfile2.txt"
    Thanks
    HN
    Last edited by Rems; 14th February 2014, 01:00. Reason: added [CODE] tags 'round the code

  • #2
    Re: Search file content with special char

    You should be able to "escape" it using a open single quote:
    http://ss64.com/ps/syntax-esc.html
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: Search file content with special char

      Originally posted by Ossian View Post
      You should be able to "escape" it using a open single quote:
      http://ss64.com/ps/syntax-esc.html
      Thank you Sir!!!
      I think I got it.. "[$]"

      Comment


      • #4
        Re: Search file content with special char

        Originally posted by Humannetwork View Post
        Thank you Sir!!!
        I think I got it.. "[$]"
        Yes this might work well for you, but note that it is not the same as "*$" from your previous code.

        Using square brackets here means that at least one of the characters placed between the square brackets must match.

        If you search for "myas$" the expression should be {$_ -match "myas\$"}. While "[myas$]" would match each line containing an m, y, a, s or $.

        If you want to use the wildcard then this character must also be escaped:
        Code:
        get-content "c:\myfile.txt" | `
        Where {$_ -match "\*\$"} | `
        Set-Content "c:\myfile2.txt"
        /Rems
        Last edited by Rems; 14th February 2014, 01:28.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment

        Working...
        X