I have two .txt documents and I would like to search in one of them for a keys from the secoond file. If the keys are found, I want to replace the match with the text that follows the key in the second file. Also, I would like to add a "T" or a "B" depending on the number in the file.

Here is an example of a first file:

```
   1       0010    SOMETEXT/NUMBERS    SOME TEXT, TEXT
   .2      0020    SOMETEXT/NUMBERS    SOME TEXT, TEXT, TEXT
   1       0020A   SOMETEXT/NUMBERS    SOME TEXT,TEXT
   .1      2000    SOMETEXT/NUMBERS    SOME TEXT,TEXT, TEXT
   ..3     9123A   SOMETEXT/NUMBERS    SOME TEXT
   ...     ...     ...                 ...

```

Here is an example of a second file:

```
  0010    ABCD EFG
  0020    C01
  0020A   C02, C3, C004, C9901, C938, C12
  2000    R123, R13, D34 
  9123A   SOMETEXT
  ...     ...

```

From these two files, I would like to use the "keys" from the second file (at the start of every new line). So in this example the keys would be: 0010, 0020, 0020A, 2000, 9123A. After I have the keys I want to search the first text document in the second column to see if there is a match. If there is a match (in this case they all match) I would like to replace the text in the first document and create a new document with the updated text. Also, if there are multiple values after the keys in the second file, I would like to copy the lines down. Look below to see what I mean.

The new document would look like this:

```
   1       ABCD EFG   SOMETEXT/NUMBERS    SOMETEXT, TEXT
   .2      C01        SOMETEXT/NUMBERS    SOME TEXT, TEXT, TEXT
   1       C02        SOMETEXT/NUMBERS    SOME TEXT,TEXT             //--------------------
   1       C3         SOMETEXT/NUMBERS    SOME TEXT,TEXT             // Copied the lines
   1       C004       SOMETEXT/NUMBERS    SOME TEXT,TEXT             // down if there are
   1       C9901      SOMETEXT/NUMBERS    SOME TEXT,TEXT             // multiple values in
   1       C938       SOMETEXT/NUMBERS    SOME TEXT,TEXT             // document 2.
   1       C12        SOMETEXT/NUMBERS    SOMETEXT,TEXT              //--------------------
   .1      R123       SOMETEXT/NUMBERS    SOME TEXT,TEXT, TEXT       //--------------
   .1      R13        SOMETEXT/NUMBERS    SOME TEXT,TEXT, TEXT       // COPIED ALSO
   .1      D34        SOMETEXT/NUMBERS    SOME TEXT,TEXT, TEXT       //--------------
   ..3     SOMETEXT   SOMETEXT/NUMBERS    SOME TEXT
   ...     ...     ...                 ...

```

Also, if the key(s) end with an "A" I would like to add a "T" to the end of the string above. If the key(s) do not end with an "A" I would like to add a "B" to the end of the string above.

Example of a few lines:

```
   .1      R123       SOMETEXT/NUMBERS    SOME TEXT,TEXT, TEXT         B       //key was 2000
   .1      R13        SOMETEXT/NUMBERS    SOME TEXT,TEXT, TEXT         B       //key was 2000
   .1      D34        SOMETEXT/NUMBERS    SOME TEXT,TEXT, TEXT         B       //key was 2000
   ..3     SOMETEXT   SOMETEXT/NUMBERS    SOME TEXT                    T       //key was 9123A

```

---

---

**QUESTION(S):**

* How would I search one file for keys from a second file?
	+ Once located, how do I replace the item in the first text with an item in the second text?
* After I replace the file, must I re-concatenate the string?
* After each line is correct, is there a 'best' way to add a "T" or "B" at the end of each line (this depends on the key having an "A" or not)?
* Any other recommendations to go about this?

---

Here is some code I have been trying to work with.. however I cannot get it how I want and the code is not working how I expected it to.:

```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Collections;

namespace BOM_Formatter
{
  public partial class bomForm : Form
  {
    // Create OpenFileDialogs to request paths and file names to open.
    OpenFileDialog openFile = new OpenFileDialog();
    OpenFileDialog openRefs = new OpenFileDialog();

    // Create a SaveFileDialog to request a path and file name to save.
    SaveFileDialog saveFile = new SaveFileDialog();

    string filePath = "C:\\Users\\FileText.txt";
    string refsPath = "C:\\Users\\RefsText.txt";

    public bomForm()
    {
        InitializeComponent();
    }

    //*********************************************************
    //******************* OPEN FILE BUTTON ********************
    //*********************************************************
    private void openButton_Click(object sender, EventArgs e)
    {
        // Initialize the OpenFileDialog to specify the .txt extension.
        openFile.DefaultExt = "*.txt";
        openFile.Filter = ".txt Files|*.txt";
        openFile.RestoreDirectory = true;

        try
        {
            // Open the contents of the file into the originalTextRichTextBox.
            if (openFile.ShowDialog() == DialogResult.OK && openFile.FileName.Length > 0)
                originalTextRichTextBox.LoadFile(openFile.FileName, RichTextBoxStreamType.PlainText);

            // Throws a FileNotFoundException otherwise.
            else
                throw new FileNotFoundException();

            // Calls to format the opened file.
            openFileFormatHelper();

            formattedTextRichTextBox.SaveFile(filePath, RichTextBoxStreamType.PlainText);
        }

        // Catches an exception if the file was not opened.
        catch (Exception)
        {
            MessageBox.Show("There was not a specified file path.", "Path Not Found Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    //*********************************************************
    //******************** OPEN REFS BUTTON *******************
    //*********************************************************
    private void openRefsButton_Click(object sender, EventArgs e)
    {
        // Initialize the OpenFileDialog to specify the .txt extension.
        openRefs.DefaultExt = "*.txt";
        openRefs.Filter = ".txt Files|*.txt";
        openRefs.RestoreDirectory = true;

        try
        {
            // Open the contents of the file into the originalTextRichTextBox.
            if (openRefs.ShowDialog() == DialogResult.OK && openRefs.FileName.Length > 0)
                refsTextRichTextBox.LoadFile(openRefs.FileName, RichTextBoxStreamType.PlainText);

            // Throws a FileNotFoundException otherwise.
            else
                throw new FileNotFoundException();

            // Calls to format the opened refs file.
            openRefsFormatHelper();

            formattedRefsTextRichTextBox.SaveFile(refsPath, RichTextBoxStreamType.PlainText);
        }

        // Catches an exception if the file was not opened.
        catch (Exception)
        {
            MessageBox.Show("There was not a specified file path.", "Path Not Found Error",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    //*********************************************************
    //******************* SAVE FILE BUTTON ********************
    //*********************************************************
    private void saveButton_Click(object sender, EventArgs e)
    {
        // Initialize the SaveFileDialog to specify the .txt extension for the file.
        saveFile.DefaultExt = "*.txt";
        saveFile.Filter = ".txt Files|*.txt";
        saveFile.RestoreDirectory = true;

        try
        {
            // Save the contents of the formattedTextRichTextBox into the file.
            if (saveFile.ShowDialog() == DialogResult.OK && saveFile.FileName.Length > 0)
                finalTextRichTextBox.SaveFile(saveFile.FileName, RichTextBoxStreamType.PlainText);

            // Throws a FileNotFoundException otherwise.
            else
                throw new FileNotFoundException();
        }

        // Catches an exception if the file was not saved.
        catch (Exception)
        {
            MessageBox.Show("There was not a specified file path.", "Path Not Found Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    //*********************************************************
    //******************* CLEAR FILE BUTTON *******************
    //*********************************************************
    private void clearButton_Click(object sender, EventArgs e)
    {
        try
        {
            // Resets the text in all of the boxes.
            originalTextRichTextBox.ResetText();
            formattedTextRichTextBox.ResetText();
            refsTextRichTextBox.ResetText();
            formattedRefsTextRichTextBox.ResetText();
            finalTextRichTextBox.ResetText();
        }

        // Catches an exception if the either text box could not be cleared.
        catch (Exception)
        {
            MessageBox.Show("Could not clear the text.", "Clearing Text Box Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    //*********************************************************
    //******************* FORMAT FILE BUTTON ******************
    //*********************************************************
    private void formatButton_Click(object sender, EventArgs e)
    {
        try
        {
           //var list = new List<string>();
            openFileFormatHelper();
            openRefsFormatHelper();
           // formatAllHelper();

            finalFormatHelper();
        }

        // Catches an exception if the file was not opened.
        catch (Exception)
        {
            MessageBox.Show("Could not format text.", "Text Formatting Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    private void finalFormatHelper()
    {
        try
        {
            string[] openFileFinal = File.ReadAllLines(filePath);
            string[] openRefsFinal = File.ReadAllLines(refsPath);

            var result = new List<string>();
            string theOutput;
           // StringBuilder finalOutput = new StringBuilder();

            foreach (var fileLines in openFileFinal)
            {
                //Match numberStartMatch = Regex.Match(fileLines, @"^[\d]+");//[\s+[\d\w]+]*");
                var fileParts = fileLines.Split(' ');

                foreach (var refLines in openRefsFinal)
                {
                    var refsParts = refLines.Split(' ');
                    theOutput = fileParts.ToString();

                    if (fileParts[1].Equals(refsParts[0]))
                    {
                        theOutput = fileParts[0] + "\t" + fileParts[1].Replace(fileParts[1], refsParts[1]) 
                            + "\t" + fileParts[2] + "\t" + fileParts[3] + "\t" + fileParts[4];
                        result.Add(string.Join(" ", theOutput));
                    }
                    else
                        result.Add(theOutput);
                }
               // finalTextRichTextBox.AppendText(result.ToString());
                // Otherwise keep the line how it is.
            }
            foreach (var line in result)
            {
                finalTextRichTextBox.AppendText(line + "\n");
            }
        }

        // Catches an exception if the file could not be formatted.
        catch (Exception)
        {
            MessageBox.Show("There was a problem formatting the 'Final File'.", "Final File Format Error",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    //*********************************************************
    //******************* OPEN FILE HELPER ********************
    //*********************************************************
    private void openFileFormatHelper()
    {
        try
        {
            // Resets the formattedTextRichTextBox so multiple files aren't loaded on top of eachother.
            formattedTextRichTextBox.ResetText();

            foreach (string line in File.ReadAllLines(openFile.FileName))
            {
                // Uses regular expressions to find a line that has, digit(s), space(s), digit(s) + letter(s),
                // space(s), digit(s), space(s), any character (up to 25 times).
                Match theMatch = Regex.Match(line, @"^[\.*\d]+\s+[\d\w]+\s+[\d\-\w*]+\s+.{25}");

                if (theMatch.Success)
                {
                    // Stores the matched value in string output.
                    string output = theMatch.Value;

                    // Replaces tabs and extra space with only 1 space delimiter
                    output = Regex.Replace(output, @"\s+", " ");

                    // Sets the formattedTextRichTextBox to the string output.
                    formattedTextRichTextBox.AppendText(output);
                    formattedTextRichTextBox.AppendText("\n");
                }
            }
        }

        // Catches an exception if the file was not opened.
        catch (Exception)
        {
            MessageBox.Show("There was a problem formatting the 'Open File'.", "Open File Format Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        // Creates a final string of the formatted rich text box.
        //string openFileFinal = formattedTextRichTextBox.Text.ToString();
    }

    //*********************************************************
    //******************* OPEN REFS HELPER ********************
    //*********************************************************
    private void openRefsFormatHelper()
    {
        try
        {
            formattedRefsTextRichTextBox.ResetText();
            // Reads the lines in the file to format.
            var reader = File.OpenText(openRefs.FileName);

            // Creates a list for the lines to be stored in.
            var list = new List<string>();

            // Adds each line in the file to the list.
            while (true)
            {
                var line = reader.ReadLine();
                if (line == null) 
                    break;
                list.Add(line);
            }

            // Handles all of the requirements for the reference text.
            list = fourDigitRequirement(list);
            list = concatRequirement(list);
            list = startsWithBRequirement(list);
            list = elementIsBRequirement(list);
            list = removeTRequirement(list);

            // Prints the formatted refs to the richtextbox.
            foreach (var line in list)
                formattedRefsTextRichTextBox.AppendText(line + "\n");

            // Creates a final string of the formatted refs rich text box.
          //string refsFileFinal = formattedRefsTextRichTextBox.Text.ToString();
        }

        // Catches an exception if the file could not be formatted.
        catch (Exception)
        {
            MessageBox.Show("There was a problem formatting the 'Refs File'.", "Refs File Format Error",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    //*********************************************************
    //****************** FORMAT ALL HELPER ********************
    //*********************************************************
   /* private void formatAllHelper()
    {
        try
        {
            finalTextRichTextBox.ResetText();

            // Reads the lines in the file to format.
            var fileReader = File.OpenText(formattedTextRichTextBox.Text);
            var refsReader = File.OpenText(formattedRefsTextRichTextBox.Text);

            // Creates a list for the lines to be stored in.
            var list = new List<string>();
            var list2 = new List<string>();

            // Adds each line in the file to the list.
            while (true)
            {
                var line = fileReader.ReadLine();
                var line2 = refsReader.ReadLine();
                if (line == null)
                    break;
                if (line2 == null)
                    break;

                list.Add(line);
                list2.Add(line2);
            }

            list = finalTextRequirement(list, list2);

            // Prints the formatted refs to the richtextbox.
            foreach (var line in list)
                finalTextRichTextBox.AppendText(line + "\n");
        }

        // Catches an exception if the file could not be formatted.
        catch (Exception)
        {
            MessageBox.Show("There was a problem formatting the 'Final Text' file.", "Final File Format Error",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    public List<string> finalTextRequirement(List<string> list, List<string> list2)
    {
        // Creates variables of the text in the two rich text boxes.
        var openFileFinal = File.ReadAllLines(formattedTextRichTextBox.Text);
        var openRefsFinal = File.ReadAllLines(formattedRefsTextRichTextBox.Text);
        StringBuilder finalOutput = new StringBuilder();
        var result = new List<string>();

        try
        {
            finalTextRichTextBox.ResetText();

            // Splits the text
            var openFileFinalLines = openFileFinal.Select(f => f.Split(' ')).ToDictionary(g => g[1], h => h);
            var openRefsFinalLines = openRefsFinal.Select(f => f.Split(' ')).ToDictionary(g => g[0], h => h[1].Split(','));

            foreach (var line in openFileFinalLines)
            {
                if (openRefsFinalLines.ContainsKey(line.Key))
                    finalOutput.Append(Combine(line.Value, openRefsFinalLines[line.Key]));

                else
                    finalOutput.AppendLine(string.Join("\t", line.Value));
            }
        }

        // Catches an exception if the final output could not be created.
        catch (Exception)
        {
            MessageBox.Show("There was a problem creating the final document.", "Final Text Formatting Error",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        finalTextRichTextBox.Text = finalOutput.ToString();
        return result;
        /*
        // Creates a new list to return with new format.
        var result = new List<string>();
        var openFileFinal = File.ReadAllLines(formattedTextRichTextBox.Text);

        foreach (var line in openFileFinalLines)
        {
            if (openRefsFinalLines.ContainsKey(line.Key))
                finalOutput.Append(Combine(line.Value, openRefsFinalLines[line.Key]));

            else
                finalOutput.AppendLine(string.Join("\t", line.Value));
        }
    }

    string Combine(string[] openFileFinalLines, string[] openRefsFinalLines)
    {
        StringBuilder aBuilder = new StringBuilder();

        foreach (var theString in openRefsFinalLines)
        {
            aBuilder.AppendLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}", openFileFinalLines[0], theString, openFileFinalLines[2],
                openFileFinalLines[3], openFileFinalLines[1].EndsWith("A") ? "T" : "B"));
        }

        return aBuilder.ToString();
    }*/

    //*************************************************************
    //***************** ELEMENT IS B REQUIREMENT ******************
    //*************************************************************
    // This handles the requirement of the second element in the list being a "B".
    static List<string> elementIsBRequirement(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // Splits each line into 'parts'
            var parts = line.Split(' ');

            // Checks if the second 'part' array is "B"
            if (parts[1].Equals("B"))
            {
                // If it is "B", replace with "A" and add to the new list "result"
                parts[0] += "A";
                parts[1] = string.Empty;

                result.Add(string.Join(" ", parts));
            }

            // Otherwise keep the line how it is.
            else
                result.Add(line);
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

    //*************************************************************
    //***************** STARTS WITH B REQUIREMENT *****************
    //*************************************************************
    // This handles the requirement of the element starting with a "B".
    static List<string> startsWithBRequirement(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();
        var i = 0;

        // Checks if the line begins with "B"
        foreach (var line in list)
        {
            // Splits each line into 'parts'
            var parts = line.Split(' ');

            // Checks if the first 'part' array is "B"
            if (parts[0].Equals("B"))
            {
                // If it is "B", copy the previous line down and add "A" where "B" was at
                // and add to the new list "result"
                parts[0] = string.Empty;
                result.Add(list[i - 1].Split(' ')[0] + "A" + string.Join(" ", parts));
            }

            // Otherwise keep the line how it is.
            else
                result.Add(line);

            i++;
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

    //*************************************************************
    //****************** CONCAT LIST REQUIREMENT ******************
    //*************************************************************
    // This handles the requirement of concatting the list.
    static List<string> concatRequirement(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // Splits each line into 'parts'
            var parts = line.Split(' ');
            int test;

            // Concats everything together
            if (int.TryParse(parts[0], out test) || parts[0].Equals("B"))
                result.Add(line);

            // Otherwise result - 1
            else
                result[result.Count - 1] += line;
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

    //*************************************************************
    //***************** REMOVE THE T REQUIREMENT ******************
    //*************************************************************
    // This handles the requirement of removing the "T".
    static List<string> removeTRequirement(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // Splits each line into 'parts'
            var parts = line.Split(' ');

            // Checks if the second 'part' array is "T", if it is, remove "T"
            if (parts[1].Equals("T"))
                parts[1] = string.Empty;

            // Add the new string to the result.
            result.Add(string.Join(" ", parts).Replace("  ", " "));
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

    //*************************************************************
    //****************** FOUR DIGITS REQUIREMENT ******************
    //*************************************************************
    // This handles the requirement of the digits being a certain length.
    static List<string> fourDigitRequirement(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // Splits each line into 'parts'
            var parts = line.Split(' ');
            int test;

            // Checks if the array[0] (digits) is the proper length.
            if (int.TryParse(parts[0], out test))
            {
                // If it is not a length of 4 digits, add "O" to the front until it is.
                parts[0] = parts[0].PadLeft(4, '0');

                // Add the new string to the result list.
                result.Add(string.Join(" ", parts));
            }

            // Otherwise keep the line how it is.
            else
                result.Add(line);
        }

        // Returns the new list so it can be formatted further.
        return result;
    }
  }
}

```

---

Right now I have to save both text boxes to a .txt file before I can 'upload' them into the final format button to completely format and merge the files. Is there anyway to not have to save the files as a hardcopy but rather upload them from both the formattedTextRichTextBox and the formattedRefsRichTextBox to be processes for more formatting?