On May 21, 4:54 pm, Kryten <Kryte...@xxxxxx> wrote:
Quote:
> Hi,
>
> I have a .txt file that contains:-
>
> CSC120 20/05/08 16:55:39 2376032
> OPRDATA: 28 0 4 15 57540
> garbage
> garbage
> garbage
> CSC120 20/05/08 16:55:42 2376033
> OPRDATA: 68 1 6 15 53218
> garbage
> garbage
>
> And so on.
>
> I'm trying to find a way to match the entirety of the two lines that
> begin 'CSC' and 'OPRDATA' but I want a single match for both these
> lines. I'd like to use a regex for this.
>
> I could probably do a 'foreach' with an 'if' to iterate through all
> the lines, and I could also just grab all the line matches for 'CSC'
> then all the line matches for 'OPR' then bolt them together - but I
> want to use a regex as it will be more extensible for other things
> that I want to do.
>
> In the hex view of the .txt I can see an '0D 0A" at the end of the
> 'CSC' line so I've tried a regex featuring CRLF but can't seem to find
> a way to get the match to span both lines.
>
> Would appreciate any help.
>
> Thanks
>
> Stuart
Stuart,
You will need to look at your file as one string:
$contents = ( Get-Content file.txt ) | Out-String
Once you have that, the regular expression is pretty simple:
[regex]::Matches( $contents, "CSC[^\n]*\s+OPRDATA[^\n]*" ) |
Foreach-Object { $_.Value }
I hope that helps.
Jeff