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