Results 1 to 7 of 7

Thread: Get line number that contains a string

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    Get line number that contains a string

    How can I get a line number that contains a specified string in a text file and write a word one line below it?

  2. #2
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: Get line number that contains a string

    With a little finagling anything is possible.

    vb Code:
    1. Dim file = System.IO.File.ReadAllLines("Your Document")
    2.         Dim newfile = My.Computer.FileSystem.OpenTextFileWriter("Your Document", False)
    3.         Dim line As Integer = Array.Find(file, Function(s) s.Contains("searchstring"))
    4.         For x = 0 To line
    5.             newfile.WriteLine(file(x).ToString)
    6.         Next
    7.         newfile.WriteLine("Your new Line")
    8.         For y = (line + 1) To file.Length - 1
    9.             newfile.WriteLine(file(y).ToString)
    10.         Next
    11.         newfile.Close()

    something like that should work

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    Re: Get line number that contains a string

    Quote Originally Posted by Zmcpherson View Post
    With a little finagling anything is possible.

    vb Code:
    1. Dim file = System.IO.File.ReadAllLines("Your Document")
    2.         Dim newfile = My.Computer.FileSystem.OpenTextFileWriter("Your Document", False)
    3.         Dim line As Integer = Array.Find(file, Function(s) s.Contains("searchstring"))
    4.         For x = 0 To line
    5.             newfile.WriteLine(file(x).ToString)
    6.         Next
    7.         newfile.WriteLine("Your new Line")
    8.         For y = (line + 1) To file.Length - 1
    9.             newfile.WriteLine(file(y).ToString)
    10.         Next
    11.         newfile.Close()

    something like that should work
    It resulted a blank text file.

    I could be wrong but isn't that code works with two text files? (correct me if i'm wrong :P) I don't want to work with two text files.
    Last edited by reinhold; May 22nd, 2012 at 10:12 PM.

  4. #4
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: Get line number that contains a string

    Sorry I was slightly off... instead of Array.find() it should be Array.FindIndex()...

    This only uses one text file... what this does is that it ReadsAllLines from the original text and saves it to an array, it then searches through the array for the specified line you are looking for, and then it prints the array to the same document after it deletes all the contents....

    I was able to get this to work perfectly...

    vb Code:
    1. Dim file = System.IO.File.ReadAllLines("C:\doc.txt")
    2.         Dim newfile = My.Computer.FileSystem.OpenTextFileWriter("C:\doc.txt", False)
    3.         Dim line As Integer = Array.FindIndex(file, Function(s) s.Contains("hello"))
    4.         For x = 0 To line
    5.             newfile.WriteLine(file(x).ToString)
    6.         Next
    7.         newfile.WriteLine("This is my new line")
    8.         For y = (line + 2) To file.Length - 1
    9.             newfile.WriteLine(file(y).ToString)
    10.         Next
    11.         newfile.Close()

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    Re: Get line number that contains a string

    Quote Originally Posted by Zmcpherson View Post
    Sorry I was slightly off... instead of Array.find() it should be Array.FindIndex()...

    This only uses one text file... what this does is that it ReadsAllLines from the original text and saves it to an array, it then searches through the array for the specified line you are looking for, and then it prints the array to the same document after it deletes all the contents....

    I was able to get this to work perfectly...

    vb Code:
    1. Dim file = System.IO.File.ReadAllLines("C:\doc.txt")
    2.         Dim newfile = My.Computer.FileSystem.OpenTextFileWriter("C:\doc.txt", False)
    3.         Dim line As Integer = Array.FindIndex(file, Function(s) s.Contains("hello"))
    4.         For x = 0 To line
    5.             newfile.WriteLine(file(x).ToString)
    6.         Next
    7.         newfile.WriteLine("This is my new line")
    8.         For y = (line + 2) To file.Length - 1
    9.             newfile.WriteLine(file(y).ToString)
    10.         Next
    11.         newfile.Close()
    I really appreciate what you're doing to help me so far but there are still minor things i'd like to address. I hope you don't mind.

    1)It finds the word, goes to one line below it but if any text if present there, it just replaces it. For example this is our hypotetical text:

    ...
    [apes]
    bonobo
    chimp
    [wildcats]
    leopar
    cheetah
    tiger
    lion
    [wildbirds]
    eagle
    falcon
    ...

    VB Code:
    1. Dim file = System.IO.File.ReadAllLines("C:\doc.txt")
    2.             Dim newfile = My.Computer.FileSystem.OpenTextFileWriter("C:\doc.txt", False)
    3.             Dim line As Integer = Array.FindIndex(file, Function(s) s.Contains("[wildcats]"))
    4.             For x = 0 To line
    5.                 newfile.WriteLine(file(x).ToString)
    6.             Next
    7.             newfile.WriteLine("bobcat")
    8.             For y = (line + 2) To file.Length - 1
    9.                 newfile.WriteLine(file(y).ToString)
    10.             Next
    11.             newfile.Close()

    End result is this: (leopar is replaced)

    ...
    [apes]
    bonobo
    chimp
    [wildcats]
    bobcat
    cheetah
    tiger
    lion
    [wildbirds]
    eagle
    falcon
    ...


    2)If the phrase i'm looking for is in the first line of the file then it doesn't write one line below it, it replaces the phrase i'm looking for with the one i write.

    So far these two issues came up.

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Get line number that contains a string

    Hello,

    If this is not a school project continue.

    The following example reads a text file into a List(Of String), shows it in a ListBox purely for debug purposes, otherwise everything is done against the list.

    User selects an item from the ListBox (using a ListBox rather than a TextBox here to control things, feel free to use a TextBox), locate it then insert the time below it.

    Second operation is to save the file back to disk as in Button2.

    Code:
    Public Class Form1
        Private FileContents As New List(Of String)
        Private FileName As String = IO.Path.Combine(Application.StartupPath, "TextFile1.txt")
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            FileContents = IO.File.ReadLines(FileName).ToList
            ListBox1.DataSource = FileContents
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            '
            ' Use a TextBox once you see this works. When using text
            ' in a TextBox you might consider things like casing of text.
            '
            Dim Position = FileContents.IndexOf(ListBox1.Text)
            If Position <> -1 Then
                ListBox1.DataSource = Nothing
                FileContents.Insert(Position + 1, Now.ToShortTimeString)
                ListBox1.DataSource = FileContents
            End If
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            IO.File.WriteAllLines(FileName, FileContents)
        End Sub
    End Class

  7. #7
    Addicted Member
    Join Date
    Sep 2011
    Location
    Seattle
    Posts
    218

    Re: Get line number that contains a string

    Kevin is right, the more you try to solve all these things for your self the better you are going to do if this is for a class.

    Go back and look through my code and figure out what is going on and when it is being printed.... Give it a good 5 minute look and you should be able to pick out where it went wrong... Dont look below until you figure it out on your own...











    The first thing that happens is that it prints lines 1 to the selected line, then it prints your newline, then it goes to the selected "line + 1" and starts printing again. Because it said "line+2" it skipped the following line making it look like it was replaced.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width