# Select-String by pattern then if found actions

**URL:** <https://forums.powershell.org/t/select-string-by-pattern-then-if-found-actions/11105>\
**Category:** PowerShell Help\
**Created:** [August 9, 2018, 10:55am UTC](https://forums.powershell.org/t/select-string-by-pattern-then-if-found-actions/11105 "2018-08-09T10:55:47Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [August 21, 2018, 12:41pm UTC](https://forums.powershell.org/t/select-string-by-pattern-then-if-found-actions/11105/21 "2018-08-21T12:41:53Z")

</div>

PJ,

I don’t have your log to test but, some of your logic is a little hard to follow. Here’s what I think should work.

&nbsp;

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

$FullSelect += $SawLog -match "Part( Started -| Complete)|PRT(0004|0005|0009)|ENG(0029|0037)|OPR0078|WaitingForPrinterTrigger"

$GetData = $False

$Results = foreach ($line in ($FullSelect -split [environment]::NewLine)) {
    
    if ($line -match 'Part Started -') {
        #Start flag to gather data, reset array and reset $trap flag
        $GetData = $True
        $ProcessArray = @()
        $Trap = $False
    }

    if ($GetData) {
        #Return the line and check for error to set trap
        if ($line -match "PRT(0004|0005|0009)|ENG(0029|0037)|OPR0078|WaitingForPrinterTrigger") {
            $trap = $True 
        }
        $ProcessArray += $line 
    }
    
    if ($line -match "Part Complete") {
        #reset the get data flag
        $GetData = $False
        if ($Trap) { $ProcessArray }
    }
}

$Results
```

---

<div class="post-metadata">

**Author:** ![drakonis](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@drakonis](https://forums.powershell.org/u/drakonis)\
**Post date:** [August 21, 2018, 12:57pm UTC](https://forums.powershell.org/t/select-string-by-pattern-then-if-found-actions/11105/22 "2018-08-21T12:57:55Z")

</div>

That works. Seems I like to over itemize tasks in the thought process of what the pipe is doing (pluralsight vids preach one task at a time). Building scripts to perform command like functions seems easier but parsing strings is looping and pipe intensive. Definitely learned on this script I didnt have any idea I could flag a line in a loop like that and started off with thinking I needed to define a start and end post error string match. Any who thank you all for the assistance hope this base gets me what I need to parse away for my ever growing WPF script.

---

<div class="post-metadata">

**Author:** ![drakonis](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@drakonis](https://forums.powershell.org/u/drakonis)\
**Post date:** [August 21, 2018, 3:00pm UTC](https://forums.powershell.org/t/select-string-by-pattern-then-if-found-actions/11105/23 "2018-08-21T15:00:40Z")

</div>

One last tid bit hopefully an easy one. The Foreach inverted the entry order as in the list is now newest on top instead of bottom. Is there an easy way to invert that?

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/select-string-by-pattern-then-if-found-actions/11105/24 "2024-05-16T20:34:30Z")

</div>



[Previous page](https://forums.powershell.org/t/select-string-by-pattern-then-if-found-actions/11105.md?page=1)
