'Merge two big files

I need to join two text files together, but I don't just want to add one to the other, but rather add the lines of the first file until I find a word, then the same for the second, then back to the first and continue the cycle until I run out of both files .

I have the following code, which works (but takes a long time) with files of around 50k lines, but the files I need to merge are around 2kk lines.

Private Sub Juntar_Click(sender As Object, e As EventArgs) Handles Juntar.Click
        Gravar.Enabled = False
        System.IO.File.Delete("c:\temp\tempfile.txt")

        Do Until (Prog_1_Button.Enabled = True And Prog_2_Button.Enabled = True)
            While Not (Prog_1_Button.Enabled)
                lines = System.IO.File.ReadAllLines(file1).ToList
                arrayLines = lines.ToArray
                Dim i As Integer = lines.IndexOf(Array.Find(arrayLines, Function(x) (x.Contains("teste"))))
                saida = lines.GetRange(0, i + 1)
                lines.RemoveRange(0, i + 1)

                System.IO.File.WriteAllLines(file1, lines)
                If i >= 0 Then
                    Prog_Bar.Value = Prog_Bar.Value + i
                    Exit While
                Else
                    saida = lines
                    Prog_1_Button.Enabled = True
                End If
            End While
            System.IO.File.AppendAllLines("c:\temp\tempfile.txt", saida)
            saida.Clear()
            While Not (Prog_2_Button.Enabled)
                lines = System.IO.File.ReadAllLines(file2).ToList
                arrayLines = lines.ToArray
                Dim i As Integer = lines.IndexOf(Array.Find(arrayLines, Function(x) (x.Contains("teste"))))
                saida = lines.GetRange(0, i + 1)
                lines.RemoveRange(0, i + 1)

                System.IO.File.WriteAllLines(file2, lines)
                If i >= 0 Then
                    Prog_Bar.Value = Prog_Bar.Value + i
                    Exit While
                Else
                    saida = lines
                    Prog_2_Button.Enabled = True
                End If
            End While

            System.IO.File.AppendAllLines("c:\temp\tempfile.txt", saida)
            saida.Clear()
        Loop
        Gravar.Enabled = True

    End Sub

Example:

**file_1:**
aaa1
bbb1
**teste**1
ccc1
ddd1
**teste**1


**file_2:**
aaa2
bbb2
**teste**2
ccc2
ddd2
**teste**2

**output:**
aaa1
bbb1
**teste**1
aaa2
bbb2
**teste**2
ccc1
ddd1
**teste**1
ccc2
ddd2
**teste**2


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source