Finding string

Tagged: 

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Tony Antony Tony Antony 1 month, 2 weeks ago.

  • Author
    Posts
  • #63929
    Profile photo of Tony Antony
    Tony Antony
    Participant

    Hello,

    I know there's Select-String, and -cmatch to find strings. My understanding is that Select-String is not very strict, and -cmatch is the best option to find exact matches since you can use regex.

    My question is, does -cmatch have a .LineNumber option like Select-String?

    Thank you,

    Tony

  • #63931
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    You can use regex with Select-String as well.

  • #63932
    Profile photo of Tony Antony
    Tony Antony
    Participant

    How would I do that?

    I have a text file with these numbers, and I'm trying to find only 2=2
    3=24
    24=24
    2=2
    12=2
    12=2
    ==================================================================

    $TXT = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\test.txt'
    $Read = Get-Content $TXT
    $Read | Select-String -CaseSensitive "^2=2$" -SimpleMatch

    If I do this, it will not show any results

    But, when I have

    $TXT = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\test.txt'
    $Read = Get-Content $TXT
    $Read -cmatch "^2=2$"

    It will show only 2=2, which is what I'm looking for

    How would I use regex on select-string?

    • #63935
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      First of all: Did you at least try to get information you need by reading the help for the cmdlet you're about to use?

      Get-Help Select-String -Full
      Get-Help Select-String -ShowWindow
      Get-Help Select-String -Online

      On top of this there are thousands of examples on the internet.
      Now to your request:

      $TXT = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\test.txt'
      Select-String -Path $TXT -Pattern '^2=2$' -AllMatches | Select-Object *

      Now you can choose whatever result you are looking for. But actually you don't need a regex for that. You are looking for a particular string!?

  • #63937
    Profile photo of Tony Antony
    Tony Antony
    Participant

    Thank you,

    I did look at Get-Help Select-String -Examples

You must be logged in to reply to this topic.