Reputation: 37
I would like to be able to reduce the output from my script and then if possible to query the End Code to write to the user if the End Code is correct.
This is the code im currently using:
foreach ($line in $contentt) {
Write-Host $line -BackgroundColor DarkGray -ForegroundColor Black
$date = Get-Date -format "MMM dd"
$yday = (Get-Date).AddDays(-1).ToString("MMM dd")
$output = Get-Content -Path "$line" |
Select-String -Pattern $date -SimpleMatch |
Select-String -Pattern "End Code"
$youtput = Get-Content -Path "$line" |
Select-String -Pattern $yday -SimpleMatch |
Select-String -Pattern "End Code"
if ($output -eq $null) {
Write-Host "No content in the file for today"
} else {
Write-Host $output
}
if ($youtput -eq $null) {
Write-Host "No content in the file for yesterday"
} else {
Write-Host $youtput
}
}
This is the output im currently receiving:
Jan 26, 2015 7:34:37 AM JobName - ID = 15 , End Code = 0 , time (ms) = 2046
I would like to be able to trim the output to be:
EndCode = 0
Upvotes: 0
Views: 934
Reputation: 200503
Use a regular expression replacement:
$output -replace '.*(End Code = \d+).*', '$1'
or, since you're already using Select-String
:
... | Select-String -Pattern '(End Code = \d+)' | ForEach-Object {
$_.Matches.Groups[0].Value
}
If the string contains multiple spaces before and/or after the =
change the grouped expression to (End Code += +\d+)
.
Upvotes: 1
Reputation: 1973
Your friend is a mix of Substring() and IndexOf():
$a = "Jan 26, 2015 7:34:37 AM JobName - ID = 15 , End Code = 0 , time (ms) = 2046"
$a.Substring($a.IndexOf("End"),$a.IndexOf(" , time") - $a.IndexOf("End"))
It produces End Code = 0
Upvotes: 2