locked
Search multiple words in txt file RRS feed

  • Question

  • Hello, how can i make to search multiple keywords in a txt file line by line using an txt with my keywords and make output in another txt file for example keyword used & count

        Private Sub Counter()
            Dim Lookfor() As String = (txtfilewithmykeywords.txt)
            Dim count As Integer
            For Each line As String In File.ReadAllLines("C:\Users\walle\test.txt")
                If Lookfor.IndexOf(line, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
                    count = count + 1
                End If
            Next
            Here i want to create an .txt file with keyword used and count
        End Sub




    • Edited by extream87 Tuesday, October 13, 2020 9:37 AM
    Tuesday, October 13, 2020 9:28 AM

Answers

  • Hi

    OK.  Here is another version.  This version does not do a search line by line as per your initial requirements, but deals with the LookIn as a block of text.

    Try this version out.

    ' Blank Form1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim LookInPath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFolder", "LookInFile.txt")
    	Dim LookForPath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFolder", "LookForFile.txt")
      Dim OutputPath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFolder", "Output.txt")
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Counter()
      End Sub
      Sub Counter()
        Dim tb As New TextBox
        Dim LookFor() As String = IO.File.ReadAllText(LookForPath).Split(New Char() {","c, " "c, Chr(13), Chr(10)}, StringSplitOptions.RemoveEmptyEntries)
        Dim allText() As String = IO.File.ReadAllText(LookInPath).Split(New Char() {","c, " "c, Chr(13), Chr(10)}, StringSplitOptions.RemoveEmptyEntries)
        For Each searchstring As String In LookFor
          Dim count As Integer = (From word In allText
                                  Where word.ToLowerInvariant() = searchstring.ToLowerInvariant() Select word).Count
          Dim ss As String = searchstring & " x" & count.ToString & "  "
          If count > 0 Then
            tb.AppendText(ss & vbCrLf)
          End If
        Next
        If tb.Text.Length > 0 Then
          IO.File.WriteAllLines(OutputPath, tb.Lines)
          MsgBox("Done!", MsgBoxStyle.Information)
        Else
          MsgBox("Nothing found!", MsgBoxStyle.Information)
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by extream87 Wednesday, October 14, 2020 12:54 PM
    Wednesday, October 14, 2020 11:32 AM

All replies

  • Hi

    It looks like a straightforward thing to do, but, you will need to be more specific.  An example of some random text to represent your text file to look in and some random words in your find these words file,  then,  a sample of the results you wold expect.

    *

    For example: 

    Look in:

    thiis is a test file with

    some random words and is

    only for a test with the following

    look for words list

    Look For:

    test, with, random, is, look

    test: occurs line 1 = 1, line 2 = 0, line 3 = 1, line 4 = 0

    with: occurs line 1 = 1, line 2 = 0, line 3 = 1, line 4 = 0

    random: occurs line 1 = 0, line 2 = 1, line 3 = 0, line 4 = 0

    is: occurs line 1 = 1, line 2 = 1, line 3 = 0, line 4 = 0

    look: occurs line 1 = 0, line 2 = 0, line 3 = 0, line 4 = 1

    *

    is it something like that that you want?


    Regards Les, Livingston, Scotland

    Tuesday, October 13, 2020 12:15 PM
  • Hello thanks leshay. Im sorry for not being very explicit.

    Yes its +- what i want. 

    Lets say i have target.txt with:

    Hello im Ronaldo

    Hi im Ronaldo

    Hey im Lucas

    And i have in keywords.txt with:

    ronaldo

    lucas

    I will search the words ronaldo and lucas on target.txt and the output file will be output.txt with:

    ronaldo x2

    lucas x1

    Thanks mate


    • Edited by extream87 Tuesday, October 13, 2020 6:07 PM
    Tuesday, October 13, 2020 6:07 PM
  • Hi

    OK, try this roughed out stand alone example and see if it is close to your needs.  I have a Folder on my DeskTop called TestFolder.  In this folder I have LookForFile.txt which contains any amount of lines of commas separated words - these will all be searched for., and, a file called LookInFile.txt which is the text file to be searched (line bu line) for all the words in the first file.

    You will need to adjust all of that to suit youe environment/system.

    There is a MultiLine TextBox1 on Form1 and the results are written to it.

    ' Form1 with TextBox1 (multiline)
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim LookInPath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFolder", "LookInFile.txt")
    	Dim LookForPath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFolder", "LookForFile.txt")
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim LookFor() As String = IO.File.ReadAllText(LookForPath).Split(New Char() {","c, " "c, Chr(13)}, StringSplitOptions.RemoveEmptyEntries)
        For Each line As String In IO.File.ReadAllLines(LookInPath)
          Dim sp() As String = line.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
          TextBox1.AppendText(line & ":   ")
          For Each searchstring As String In LookFor
            Dim counter As Integer = (From word In sp
                                      Where word.ToLowerInvariant() = searchstring.ToLowerInvariant() Select word).Count
            TextBox1.AppendText(searchstring & " = " & counter.ToString & "  ")
          Next
          TextBox1.AppendText(vbCrLf)
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Tuesday, October 13, 2020 6:15 PM
  • Hello thanks leshay but seems is not working properly:
    My keywords.txt have:

    Ronaldo
    romario

    And target.txt

    Ronaldo
    romario
    derlei


    Code:

        Private Sub counter()
            Dim LookInPath As String = "C:\Users\%username%\desktop\target.txt"
            Dim LookForPath As String = "C:\Users\%username%\desktop\keywords.txt"
    
            Dim LookFor() As String = File.ReadAllText(LookForPath).Split(New Char() {vbNewLine}, StringSplitOptions.RemoveEmptyEntries)
            For Each line As String In File.ReadAllLines(LookInPath)
                Dim sp() As String = line.Split(New Char() {vbNewLine}, StringSplitOptions.RemoveEmptyEntries)
                TextBox1.AppendText(line & vbNewLine)
                For Each searchstring As String In LookFor
                    Dim counter As Integer = (From word In sp
                                              Where word.ToLowerInvariant() = searchstring.ToLowerInvariant() Select word).Count
                    TextBox1.AppendText(searchstring & " x" & counter.ToString & vbNewLine)
                Next
    
                TextBox1.AppendText(vbCrLf)
            Next
        End Sub

    Output is:

    Ronaldo
    Ronaldo x1
    
    romario x0
    
    romario
    Ronaldo x0
    
    romario x0
    
    derlei
    Ronaldo x0
    
    romario x0

    Should be:

    Ronaldo x1
    romario x1


    • Edited by extream87 Wednesday, October 14, 2020 9:25 AM
    Wednesday, October 14, 2020 9:24 AM
  • Hi

    OK, try this version.  There is no Textbox on the Form this time, and, the application will write the results to a file called Output.txt to the path OutputPath, and will then terminate - so no Form will be seen.  If there are no matches then the Output file will report that.

    Give it a try.

    ' Blank Form1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim LookInPath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFolder", "LookInFile.txt")
    	Dim LookForPath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFolder", "LookForFile.txt")
      Dim OutputPath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFolder", "Output.txt")
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Counter()
      End Sub
      Sub Counter()
        Dim tb As New TextBox
        Dim LookFor() As String = IO.File.ReadAllText(LookForPath).Split(New Char() {","c, " "c, Chr(13), Chr(10)}, StringSplitOptions.RemoveEmptyEntries)
        For Each line As String In IO.File.ReadAllLines(LookInPath)
          Dim sp() As String = line.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
          For Each searchstring As String In LookFor
            Dim count As Integer = (From word In sp
                                    Where word.ToLowerInvariant() = searchstring.ToLowerInvariant() Select word).Count
            Dim ss As String = searchstring & " x" & count.ToString & "  "
            If count > 0 Then tb.AppendText(ss & vbCrLf)
          Next
        Next
        If tb.Text.Length > 0 Then
          IO.File.WriteAllLines(OutputPath, tb.Lines)
        Else
          tb.AppendText("No Matches found .....")
          IO.File.WriteAllLines(OutputPath, tb.Lines)
        End If
        End
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, October 14, 2020 10:17 AM
    Wednesday, October 14, 2020 10:17 AM
  • Thanks again leshay now is allmost working...

    Now the only problem is the output is:

    Ronaldo x1
    romario x1
    romario x1

    Should be:

    Ronaldo x1
    romario x2
    

    Code:

        Private Sub counter()
            Dim LookInPath As String = "C:\Users\%username%\desktop\test.txt"
            Dim LookForPath As String = "C:\Users\%username%\desktop\keywords.txt"
            Dim OutputPath As String = "C:\Users\%username%\desktop\output.txt"
    
            Dim tb As New TextBox
            Dim LookFor() As String = File.ReadAllText(LookForPath).Split(New Char() {vbNewLine, Chr(13), Chr(10)}, StringSplitOptions.RemoveEmptyEntries)
    
            For Each line As String In File.ReadAllLines(LookInPath)
                Dim sp() As String = line.Split(New Char() {vbNewLine}, StringSplitOptions.RemoveEmptyEntries)
                For Each searchstring As String In LookFor
                    Dim count As Integer = (From word In sp
                                            Where word.ToLowerInvariant() = searchstring.ToLowerInvariant() Select word).Count
                    Dim ss As String = searchstring & " x" & count.ToString & "  "
                    If count > 0 Then tb.AppendText(ss & vbCrLf)
    
                Next
            Next
    
            If tb.Text.Length > 0 Then
                File.WriteAllLines(OutputPath, tb.Lines)
                MsgBox("Done!", MsgBoxStyle.Information)
            Else
                MsgBox("Nothing found!", MsgBoxStyle.Information)
            End If
        End Sub

    Wednesday, October 14, 2020 10:34 AM
  • Hi

    The code you are using in your last post is NOT the code I posted in my latest version.

    It is your application, and you can edit/change as much as you like, but, do not change the code provided and make out it is not working. I think you do not have the Options set as per my posting.

    Here is what I get.

    LookInFile.txt

    Ronaldo
    romario
    derlei

    LookForFile.txt

    Ronaldo
    romario

    After run:  Output.txt

    Ronaldo x1  
    romario x1  


    Regards Les, Livingston, Scotland

    Wednesday, October 14, 2020 10:58 AM
  • leshay note you are not using duplicate names in your LookInfile.txt try to put:

    Ronaldo

    Ronaldo

    romario

    derlei

    You will get

    Ronaldo x1

    Ronaldo x1

    romario x1

    And Should be:

    Ronaldo x2

    romario x1



    • Edited by extream87 Wednesday, October 14, 2020 11:15 AM
    Wednesday, October 14, 2020 11:14 AM
  • Hi

    OK.  Here is another version.  This version does not do a search line by line as per your initial requirements, but deals with the LookIn as a block of text.

    Try this version out.

    ' Blank Form1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim LookInPath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFolder", "LookInFile.txt")
    	Dim LookForPath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFolder", "LookForFile.txt")
      Dim OutputPath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFolder", "Output.txt")
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Counter()
      End Sub
      Sub Counter()
        Dim tb As New TextBox
        Dim LookFor() As String = IO.File.ReadAllText(LookForPath).Split(New Char() {","c, " "c, Chr(13), Chr(10)}, StringSplitOptions.RemoveEmptyEntries)
        Dim allText() As String = IO.File.ReadAllText(LookInPath).Split(New Char() {","c, " "c, Chr(13), Chr(10)}, StringSplitOptions.RemoveEmptyEntries)
        For Each searchstring As String In LookFor
          Dim count As Integer = (From word In allText
                                  Where word.ToLowerInvariant() = searchstring.ToLowerInvariant() Select word).Count
          Dim ss As String = searchstring & " x" & count.ToString & "  "
          If count > 0 Then
            tb.AppendText(ss & vbCrLf)
          End If
        Next
        If tb.Text.Length > 0 Then
          IO.File.WriteAllLines(OutputPath, tb.Lines)
          MsgBox("Done!", MsgBoxStyle.Information)
        Else
          MsgBox("Nothing found!", MsgBoxStyle.Information)
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by extream87 Wednesday, October 14, 2020 12:54 PM
    Wednesday, October 14, 2020 11:32 AM
  • leshay thanks master
    Works great :)
    Wednesday, October 14, 2020 12:54 PM