string search in select statement

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of Ron Ron 6 months ago.

  • Author
    Posts
  • #62394
    Profile photo of pshelldba
    pshelldba
    Participant

    I am trying to search a string from a text file with a regex and include it with a select statement:
    Here is the string:

    C:\SQLIO>echo Thu 8/12/2016  0:15:15.25 

    This is the select statement I am trying to include it to:

     @{Name="MinLat_ms"; Expression={[int]([regex]::Match($_,"Min.{0,}?\:\s(\d+)?").Groups[1].Value)}},`
    			   @{Name="date1"; Expression={[string]([regex]::Match($_,"echo\W+").Groups[1].Value)}},` 

    date1 is the value that is not working for me, can someone please help me. thank you 🙂

  • #62398
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    I believe you've forgot simple brackets around \W+ in your pattern to create a capturing group for it.

    • #62410
      Profile photo of pshelldba
      pshelldba
      Participant

      thanks for your input, I just tried that, still the value doesnt get captured to date1. Is there anything else that I am missing?

  • #62427
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    The non-word character class \W doesn't contain all the characters necessary to successfully capture what you're looking for. You could change \W+ to . and it should capture everything after echo.

    [string]([regex]::Match($_, 'echo(.+)$').Groups[1].Value).Trim()
    
    • #62773
      Profile photo of pshelldba
      pshelldba
      Participant

      I tried what you suggested, not sure if I am doing something really stupid, date1 still turns up empty, but other regex's work in the same statement. I have included one that works below as well:

      @{Name="MinLat_ms"; Expression={[int]([regex]::Match($_,"Min.{0,}?\:\s(\d+)?").Groups[1].Value)}},`
      			   @{Name="date1"; Expression={[string]([regex]::Match($_,"echo(.+)$").Groups[1].Value).Trim()}},`
  • #62434
    Profile photo of Ron
    Ron
    Participant

    It's not clear what you are expecting from the output, but...

    [regex]::Match("C:\SQLIO>echo Thu 8/12/2016  0:15:15.25 ","(echo\W+)").groups[1].value
    echo
    • #62775
      Profile photo of pshelldba
      pshelldba
      Participant

      I am trying to read a text file, and get the date part which is part of a string containing "echo". This is then going to be assigned to date1 as part of a select statement along with other values read from the text file. part of the code below. It is to date1 that I am trying to read this string into:

      @{Name="MinLat_ms"; Expression={[int]([regex]::Match($_,"Min.{0,}?\:\s(\d+)?").Groups[1].Value)}},`
      			   @{Name="date1"; Expression={[string]([regex]::Match($_,"echo(.+)$").Groups[1].Value).Trim()}},`
    • #62778
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      hmmm ... like this?

      'echo Thu 8/12/2016  0:15:15.25' -match 'echo\s+(\w{3}\s+\d{1,2}/\d{1,2}/\d{4}\s+\d{1,2}:\d{1,2}:\d{1,2}\.\d{1,2})' | Out-Null
      Get-Date $Matches[1]
    • #62818
      Profile photo of Ron
      Ron
      Participant

      One expression for your select statement:

      [DateTime]([regex]::Match("C:\SQLIO>echo Thu 8/12/2016  0:15:15.25 ","echo\s+\w{3}\s+(\d{1,2}/\d{1,2}/\d{4}\s+\d{1,2}:\d{1,2}:\d{1,2}\.\d{1,2})").groups[1].value)

You must be logged in to reply to this topic.