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 - Return the next line of in a text file after matching the previous

Reply
 
Old 08-07-2009   #1 (permalink)
Craig


 
 

Return the next line of in a text file after matching the previous

Hi all,

Could you please help me,

I have a text log file which i pattern match using Powershell to return a
line, however the line i am actually interested in is the following line, i
cannot match this line directly as the previous line (the one i'm matching
on) denotes that this line is what is required.

--
Many Thanks for your help

My System SpecsSystem Spec
Old 08-07-2009   #2 (permalink)
Rob Campbell


 
 

RE: Return the next line of in a text file after matching the previous

using CTP3:


$line = gc file.txt | select-string "<regex>" -context 0,1
$line.context

"Craig" wrote:
Quote:

> Hi all,
>
> Could you please help me,
>
> I have a text log file which i pattern match using Powershell to return a
> line, however the line i am actually interested in is the following line, i
> cannot match this line directly as the previous line (the one i'm matching
> on) denotes that this line is what is required.
>
> --
> Many Thanks for your help
My System SpecsSystem Spec
Old 08-07-2009   #3 (permalink)
Robert Robelo


 
 

Re: Return the next line of in a text file after matching the previous

Select-String's MatchInfo objects have a 1-based LineNumber property which you can use.
Get-Content returns a 0-based Object[].
Using the MatchInfo's LineNumber values -without adjusting them- as indices for the Object[] will retrieve the line after each line that successfully matched the pattern in the log.

$log = 'log.txt'
$patt = 'find me'
$indx = Select-String $patt $log | ForEach-Object {$_.LineNumber}
(Get-Content $log)[$indx]

# v2 CTP3 makes it easier
Select-String $patt $log -Context 0, 1 | % {$_.Context.PostContext}

--
Robert
My System SpecsSystem Spec
Old 08-07-2009   #4 (permalink)
Craig


 
 

Re: Return the next line of in a text file after matching the prev

Absoultely spot on on both counts. Over joyed.

--
Many Thanks for your help


"Robert Robelo" wrote:
Quote:

> Select-String's MatchInfo objects have a 1-based LineNumber property which
> you can use.
> Get-Content returns a 0-based Object[].
> Using the MatchInfo's LineNumber values -without adjusting them- as
> indices for the Object[] will retrieve the line after each line that
> successfully matched the pattern in the log.
>
> $log = 'log.txt'
> $patt = 'find me'
> $indx = Select-String $patt $log | ForEach-Object {$_.LineNumber}
> (Get-Content $log)[$indx]
>
> # v2 CTP3 makes it easier
> Select-String $patt $log -Context 0, 1 | % {$_.Context.PostContext}
>
> --
> Robert
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Unable to Create New Line in Text File PowerShell
Read a line from a text file, without loading the entire file inmemory PowerShell
The next line in a text file PowerShell
Removing lines from a text file by matching against substrings PowerShell
return only certain lines from large text file PowerShell


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