Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Trying to -match across lines of a .txt

Reply
 
Old 05-21-2008   #1 (permalink)
Kryten


 
 

Trying to -match across lines of a .txt

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

My System SpecsSystem Spec
Old 05-21-2008   #2 (permalink)
Jeff


 
 

Re: Trying to -match across lines of a .txt

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
My System SpecsSystem Spec
Old 05-21-2008   #3 (permalink)
Kryten


 
 

Re: Trying to -match across lines of a .txt

It helps immensely Jeff.

I have to keep reminding myself that get-content is returning an
'array of strings', not one big string :-)

Thanks for keeping me right, it's doing what I want now.

Regards,

Stuart

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
-match V1 vs V2 PowerShell
Re: match against array PowerShell
Key does no match disk?!? Vista installation & setup
Files do not match Vista hardware & devices
Product key does not match... Vista installation & setup


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46