stxdb
stxdb

Reputation: 13

Find a specific line from a file and read the word below it

I wrote this code which is currently reading the string '<span class="label">The Value</span><br>' from "file.txt" and I want to modify it to find the aforementioned line and read the word below it, "753921" in this case.

Dim TextFile = IO.File.ReadAllText("file.txt")
Dim Find As String = "<span class=""label"">The Value</span><br>"
If TextFile.Contains(Find) Then
    MsgBox(Find)
Else
    MsgBox("Not Found")
End If

The file "file.txt" contains:

...
<span class="label">The Value</span><br>
753921 (it the value varies)
...

I got stuck and I don't know how to continue coding (to modify the code as I said above).

The program must be something like:

  1. Find the word "<span class=""label"">The Value</span><br>"
  2. Show in MsgBox the word on the next line (the word below the one found) - 753921

Value and lines will differ, and I can't search for value by line. The value being different, I can't look for it directly, that's why I thought this must be written in code only and I don't know how, that's why I posted here. The value will always be located below the searched word '<span class=""label"">The Value</span><br>'.

I hope you understood what I meant and I hope there is someone who can help me.

Upvotes: 0

Views: 116

Answers (2)

Andrew Morton
Andrew Morton

Reputation: 25023

If by "Value and lines will differ" you mean that there is more than one item to search for, then a simple way to do that is to have a list of the lines that trigger the capture of the next line.

Once a trigger line is found, just read the next line (if there is one) and save that.

So, given some file "C:\temp\file.txt" (it's important to always give the full path for a file in a program so that you know which file it is using):

...
<span class="label">The Value</span><br>
753921 (it the value varies)
...
abc
9876
nothing here
<span class="label">The Value</span><br>
111111 (it's a different value)
<span class="label">The Value</span><br>

and the program:

Imports System.IO

Module Module1

    Sub Main()
        Dim myFile = "C:\temp\file.txt"

        Dim triggers As New List(Of String) From {"abc", "<span class=""label"">The Value</span><br>"}

        Dim results As New List(Of String)

        Using sr As New StreamReader(myFile)
            While Not sr.EndOfStream
                Dim thisLine = sr.ReadLine()
                If triggers.IndexOf(thisLine) >= 0 Then
                    If Not sr.EndOfStream Then
                        Dim nextLine = sr.ReadLine()
                        results.Add(nextLine)
                    End If
                End If
            End While
        End Using

        Console.WriteLine(String.Join(vbCrLf, results))

        Console.ReadLine()

    End Sub

End Module

You would get the output:

753921 (it the value varies)
9876
111111 (it's a different value)

If there were a lot of triggers, then it would be worth investing some time in finding a more efficient way of detecting them than IndexOf.

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415921

Here's the basic code:

Dim Find As String = "<span class=""label"">The Value</span><br>"
Dim result As String = 
    File.ReadLines("file.txt").
        SkipWhile(Function(line) line <> Find).
        Skip(1).
        FirstOrDefault()

If result Is Nothing Then 
    MsgBox("Not Found")
Else
    MsgBox(result)
End If

This can also greatly reduce RAM use, since only one line from the file is ever in memory at a time.

Here's a quick fiddle proving it works:

https://dotnetfiddle.net/OI5D0h

But it also sounds like you're also not sure of the The Value part here. In that case:


Dim FindTemplate As String = "<span class=""label"">{0}</span><br>"
Dim Find As String = String.Format(FindTemplate, "The Value")

Dim result As String = 
    File.ReadLines("file.txt").
        SkipWhile(Function(line) line <> Find).
        Skip(1).
        FirstOrDefault()

If result Is Nothing Then 
    MsgBox("Not Found")
Else
    MsgBox(result)
End If

Upvotes: 1

Related Questions