string search in select statement

Welcome Forums General PowerShell Q&A string search in select statement

This topic contains 8 replies, has 4 voices, and was last updated by

Ron
 
Participant
1 year, 10 months ago.

  • Author
    Posts
  • #62394

    Participant
    Points: 0
    Rank: Member

    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

    Moderator
    Points: 24
    Team Member
    Rank: Member

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

    • #62410

      Participant
      Points: 0
      Rank: Member

      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

    Moderator
    Points: 24
    Team Member
    Rank: Member

    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

      Participant
      Points: 0
      Rank: Member

      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
    Ron

    Participant
    Points: 0
    Rank: Member

    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

      Participant
      Points: 0
      Rank: Member

      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

      Participant
      Points: 246
      Helping Hand
      Rank: 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
      Ron

      Participant
      Points: 0
      Rank: Member

      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)

The topic ‘string search in select statement’ is closed to new replies.