Author Posts

August 9, 2018 at 4:25 pm

I am looking to search a log file for several patterns which I have working.  What I would like to do to expand upon that is when it finds pattern X THEN search UP in the text log for a new pattern, down to the same pattern searched UP for to establish which lines to grab.   A less technical summary would be this log has hundreds to thousands of operations in it and i want the log entries for an individual set of entries for one operation IF it contains an error.  Not sure how to present the exact information being worked with without information overload.

August 9, 2018 at 4:28 pm

this is the script that finds the error entries for some context to what i want to add on to...

 


$SawLog = Get-Content "C:\temp\run.log"

$PrinterLog = "C:\temp\PrinterLog.txt"

$Date = "2018-08-07"

$PrintErrors = @()

$SortMe = @()

$NoDate = @()

$Final = @()

 

If (Test-Path $PrinterLog) {Remove-Item -Path $PrinterLog -Force}

$SawLog | %{

if ($_ -match "PRT(0004|0005|0009)|ENG(0029|0037)|OPR0078") {

if ($Matches[0] -eq "PRT0009") {

$LineIndex = $SawLog.IndexOf($_) + 1 # Get Next Line

$Output = $_, $SawLog[$LineIndex]

}

elseif ($Matches[0] -eq "\[ID: ENG0037") {

$LineIndex = $SawLog.IndexOf($_) + 3 # Get Next Line

$Output = $_, $SawLog[$LineIndex]

}

else { $Output = $_ }

$PrintErrors += $Output | Out-String

}

}

 

$SortMe += $SawLog -match "\[PRT(0004|0005|0009)|\[ID: ENG(0029|0037)|OPR0078" | Select-String -pattern $Date | %{ $List = $_ -Split '\['

$NoDate += $List[3] | Out-String  }

 

$Final += $NoDate | Group-Object -NoElement| Format-Table -AutoSize | Out-String

 

"Print Error Details `n`n" + $PrintErrors + "`n Error Summary For $Date `n" + $Final  | Set-Content -Path $PrinterLog

 

Start-Process -FilePath 'Notepad++' -ArgumentList $PrinterLog

August 9, 2018 at 4:48 pm

when it finds pattern X THEN

search UP in the text log for a new pattern,

down to the same pattern searched UP for to establish which lines to grab.

Whaaaaat???

Find X

Match X

Ignore X and replace with Y, starting from the index of X

Search up for Y and extract Y

Whaaaaat ???

What happens when string are below the X index ????

Color me really confused.

Why not just group matches for the extract.

 

August 9, 2018 at 5:00 pm

ok lets try this.  I want to select-string all lines from "Part Started" to "Part Complete" If between those two  -match "\[PRT(0004|0005|0009)|\[ID: ENG(0029|0037)|OPR0078" result is found.  does that clarify what im up to?  NOTE the number of lines between "Part Started" and "Part Complete" is not a static amount.

August 10, 2018 at 1:14 pm

So what I have found is maybe a Select-String -Context but i need to ID the lines for a line number ID to make a Start point and for an end point.  Then maybe a Select-Object -Index to pull that group to an array to then say IF -match is true keep it and {do something} else if false discard and move to next.  This process would need to function repeatedly without duplication to process the log file maybe in a foreach loop.  Does this sound plausible?