Regular Expression to include next line.

rated by 0 users
Not Answered This post has 0 verified answers | 4 Replies | 1 Follower

Not Ranked
7 Posts
ralu posted on 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 ??

 

All Replies

Top 150 Contributor
34 Posts

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.

Not Ranked
7 Posts

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
655 Posts
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]
  }
 

Not Ranked
7 Posts

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.