Regular Expression to include next line.

rated by 0 users
This post has 4 Replies | 1 Follower

Top 500 Contributor
Posts 7
ralu Posted: 04-01-2012 5:06 AM

Hello,

 

If I have a Text in a Textfile like this:

 

WARNING: Blablabla first line

blablablabla second line.

 

and I try it like this:

(get-content file) -match "^WARNING:"

I only get the first line.

 

What regular expression would include the second line ??

 

Top 100 Contributor
Posts 34

Hi there

Would you copy and paste the content, and the result that you would like to get of it?

Would you like the first and second lines only, out of an N amount of lines, perhaps?

 

Cheers.

D.

Top 500 Contributor
Posts 7

You can take the example I wrote in my qestion:

So it usually looks like this:

 

Some other Text Some other Text Some other Text Some other Text

WARNING: Blablabla first line

blablablabla second line.

Some other Text Some other Text Some other Text Some other Text

Some other Text Some other Text Some other Text Some other Text

WARNING: Blablabla first line

blablablabla second line.

 

I just want the "WARNING:*" + next line bits.......

Top 10 Contributor
Posts 633
Microsoft MVP
Top Contributor

you could try something like this:

PS> select-string $env:windir\windowsupdate.log -context 0,1 -Pattern 'WARNING'

This gives you back every line that has a "WARNING" and the immediately following line.

If you prefer to process the results from code, here's a sample that extracts the keyword line and then the following line, so you can do whatever you want with them:

select-string $env:windir\windowsupdate.log -context 0,1 -Pattern 'WARNING' |
  ForEach-Object {
    "Line with keyword: " + $_.Line
    "Following line: " + $_.Context.PostContext[0]
  }
 

Top 500 Contributor
Posts 7

As always, Tobias has solved the riddle !!

Thank you very much!

Page 1 of 1 (5 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.