Select-string

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Vishal Joshi Vishal Joshi 8 months ago.

  • Author
    Posts
  • #37463
    Profile photo of Vishal Joshi
    Vishal Joshi
    Participant

    Hi All,

    I Was trying to select a string using below code

    $Finish1 = gci C:\DataWarehouseDump*.txt | sort {$_.LastWriteTime} | select -last 1 | select-string -pattern "Finished in [0-9]+ hours [0-9]+ minutes [0-9]+ seconds with [0-9]+ error\(s\), [0-9]+ warning\(s\) and [0-9]+ truncations"

    And when i ran this code i got the below output

    DataWarehouseDump20160309-1200.txt:462:09-03-2016 13:15:19 Finished in 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations

    But here i need the output without path and date, time like below

    Finished in 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations

    to get this output i tried to split it
    $Finish3 = $finish1.tostring()
    $finish5 = $Finish3.split(".")[1]

    Now i got this output
    txt:462:09-03-2016 13:15:19 Finished in 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations
    but i dont want this text "txt:462:09-03-2016 13:15:19"

    Please help me to get the desired output.

  • #37464
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    Select-String returns a MatchInfo object. Use $Finish1.line to access the result without the line number.

    Edit:
    Sorry, just noticed you want to get rid of the date and time from the log file as well. Try this:

    $Finish1.line -replace "\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2} ",""

  • #37466
    Profile photo of Vishal Joshi
    Vishal Joshi
    Participant

    Thank You it works.
    One More QQ.
    If i want to select output like below

    in 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations.

  • #37467
    Profile photo of random commandline
    random commandline
    Participant

    This is an example of how it can work for you.

    $string = 'DataWarehouseDump20160309-1200.txt:462:09-03-2016 13:15:19 Finished in 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations'
    If ($string -match "(?'output'Finished in (?'time'.*))") {
        [pscustomobject]@{Output = $Matches['output']
            Time = $Matches['time']}}
    
    # Results
    #Output : Finished in 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations
    #Time   : 1 hours 14 minutes 57 seconds with 0 error(s), 1 warning(s) and 1 truncations
    
  • #37468
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    As you're still working with the beginning of the string, just expand the replace:

    $Finish1.line -replace "\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2} Finished ",""
    

    Alternatively, if it's always a fixed length, you could just select the sub-string:

    $Finish1.line.Substring(29)
    
  • #37469
    Profile photo of Vishal Joshi
    Vishal Joshi
    Participant

    Thanks Matt it worked

You must be logged in to reply to this topic.