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 Tutorial - The next line in a text file

Reply
 
Old 02-16-2008   #1 (permalink)
pjo
Guest


 
 

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 SpecsSystem Spec
Old 02-16-2008   #2 (permalink)
Kam-Hung Soh
Guest


 
 

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
If the line number is $c, then try this:

(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 SpecsSystem Spec
Old 02-16-2008   #3 (permalink)
Kiron
Guest


 
 

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 SpecsSystem Spec
Reply

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


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