I'm literally pulling hairs out in frustration on trying to use multiline regex, I've very little experience with Powershell, and while the examples I've tried work, as soon as I start to adjust them for what I need it never gives any results.
My text file example below
CLO*5000000Z115240057*598.50***94>0
DGP*115*G8*20161024~
DGP*096*G8*20161024~
DI*ABC>121~
QM1*BN*1*~
QM2*H2*1*~
QM1397*2*~
Q3*~
Q4*~
TX*1~
SQU*HV>01480>AB>1S>1>2>3>4~
0T1*472*D8*20160915~
RBF*6R*374196~
TX*2~
There are maybe 200 (at most) of these in the same text file. I'm searching for the line that starts with 'SQU' to contain 1>2>3>4 at the end, there are only a handful that do. I'm able to find all of the SQU lines with the code example I found below, unfortunately I need to also get the 'CLO' line as well, which is above it.
$fpath = 'C:\myfile.txt'
$opath = 'C:\logoutput.txt'
$regx = 'SQU.*1>2>3>4.*'
Get-Content $fpath | % { if($_ -match $regx) {add-content $opath $_}}
I've tried, and I've tried dozens of $'s and ^'s and ()'s all over the example below in every combination I could think of. I don't really understand how to get it into the logoutput.txt either.
$fileContent = [io.file]::ReadAllText($fpath)
$filecontent | Select-String '(?ms)CLO.*SQU.*1>2>3>4.*' -AllMatches | %{ $_.Matches } | %{ $_.Value }
and this one I tried without the >1>2>3>4 just to see if I could get anything, but no luck.
$stringmatch = Get-Content -raw $fpath
if (Select-String -inputobject $stringmatch -pattern '(?smi)CLO.*SQU.*'){
$matches[1]
}
I only need the CLO and SQU lines (if it has the 1>2>3>4) but honestly at this point I'll take the entire block if it's easier. Any help would be appreciated.