![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | The next line in a text file Hi! I have an html file which has several instances of the same string pattern "ABCDEF". $a = Select-String -Path d:\work\test.html -pattern "ABCDEF" and find the line number of the last instance of the pattern "ABCDEF" by $c = $a[$a.Length -1].ToString().Split(":")[2] $c contains the line number. How could I point to the next line number which is $c + 1 and retrieve the string of the next line to a varible ? Thanks in advance. pjo -- pjo http://www.techtalkz.com - Technology and Computer Troubleshooting Forums |
My System Specs![]() |
| | #2 (permalink) |
| | Re: The next line in a text file On Feb 17, 1:14 am, pjo <gu...@xxxxxx-email.com> wrote: Quote: > Hi! > I have an html file which has several instances of the same string > pattern "ABCDEF". > > $a = Select-String -Path d:\work\test.html -pattern "ABCDEF" > > and find the line number of the last instance of the pattern "ABCDEF" > by > > $c = $a[$a.Length -1].ToString().Split(":")[2] > > $c contains the line number. > > How could I point to the next line number which is $c + 1 and retrieve > the string of the next line to a varible ? > > Thanks in advance. > > pjo > > -- > pjohttp://www.techtalkz.com- Technology and Computer Troubleshooting Forums (get-content test.html)[$c] If you only interested in the line after the last "ABCDEF" in file test.html, try this: get-content test.html | foreach { $prev = "" } { if ($prev -match "ABCDEF") { $result = $_ }; $prev = $_ } $result should hold the value that you want. -- Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</ a> |
My System Specs![]() |
| | #3 (permalink) |
| | Re: The next line in a text file When retrieving a file's content, Get-Content returns a String object for each line in the file, if there is more than one line you get an array of String objects. When the number of elements expected is unknown --could be zero, one or more-- and you want to ensure an array, enclose the command in an array subexpression '@(...)' Select-String returns MatchInfo objects or a Boolean value. You can retrieve just one property from the MatchInfo objects --in your case the LineNumber property-- and get an array of these values. The MatchInfo object's LineNumber values are origin-one, i.e. the first line of the file, this is important to remember because PowerShell's array is origin-zero. If you get the LineNumber value of the last line that matched a pattern in the html file and would like to rerrieve the _same_ line from the array of Strings returned by Get-Content, you would have to subtract 1 from it to use it as the index. Since you want to retrieve the _next_ line just use the LineNumber value you get from Select-String as the index for the String object array. PowerShell's array notation allows negative index, i.e. -1 is the last element, -2 the next to last and so on. $htmlFile = 'd:\work\test.html' $pattern = "ABCDEF" # last match's line number $c = @(select-string $pattern $htmlFile | % {$_.lineNumber})[-1] # next line @(gc $htmlFile)[$c] # or... @(gc $htmlFile)[@(select-string $pattern $htmlFile | % {$_.lineNumber})[-1]] -- Kiron |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Unable to Create New Line in Text File | PowerShell | |||
| Return the next line of in a text file after matching the previous | PowerShell | |||
| How to read the LAST NON-BLANK line from a text file? | VB Script | |||
| Read a line from a text file, without loading the entire file inmemory | PowerShell | |||
| How to remove blanks line from a text outpu file | PowerShell | |||