![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||