Author Posts

April 9, 2016 at 2:44 am

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.

April 9, 2016 at 3:06 am

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} ",""

April 9, 2016 at 3:40 am

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.

April 9, 2016 at 4:10 am

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

April 9, 2016 at 4:21 am

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)

April 9, 2016 at 4:50 am

Thanks Matt it worked