Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Trying to -match across lines of a .txt

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-21-2008   #1 (permalink)
Kryten
Guest


 

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
Guest


 

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
Guest


 

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
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: match against array Kiron PowerShell 2 06-15-2008 07:15 AM
Filenames Do Not Match Dzomlija Vista file management 5 03-26-2008 12:19 PM
CD key does not match disk? SeekingXanadu Vista installation & setup 3 01-16-2008 04:47 AM
Files do not match Retiree Vista hardware & devices 1 08-20-2007 02:38 AM
Product key does not match... SteveO Vista installation & setup 2 02-03-2007 01:11 AM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com 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 2005-2008