Mahen dran Mahen dran - 1 month ago 8
C# Question

How to perform text matching in text file and then move that next line after text into csv file?

I need to read the text file and then check the input text in contents in text file.

If input text exists then move nextline(after input text) into csv file.

Sample Input: input.txt

Hi

Hello

-------------------------------------------------------------------------------
Code Name ID Customers CID Time. %
==================== ========= =========== ============ ===== =================

Harish SM 1001 Tower India 44.58
Siva DM 2310 Cata China 56.78



No Name ID Customers
==================== ========= ===========
MS Norway 1001 UNIBIC


If I give input "Code Name" then those 2 rows behind the CodeName will be saved in csv file upto newline.

If give input "No Name" then one row after No Name will be stored in another csv file.

Need to write code in c# only.

I'm little bit knowledge about c# console application

How can I write code to perform text file read and content into csv?.

Answer

Since the format of the input file isn't the nicest setup, we need to do a read ahead in order to get it to work. The following procedure should work to extract the lines after the given ID code into an array of strings, the first containing the identifier line.

public string[] GetDataLinesFromFile(string filename, string searchString)
    {
        List<string> dataEntries = new List<string>();

        using (System.IO.StreamReader stream = new System.IO.StreamReader(filename))
        {
            System.IO.TextReader tr = stream;

            bool foundSearchString = false;
            string lastLine = string.Empty;
            string line = string.Empty;

            while (!stream.EndOfStream)
            {
                lastLine = line;
                line = tr.ReadLine();
                if (lastLine.Trim().StartsWith(searchString) && line.Contains("===================="))
                {
                    foundSearchString = true;
                    continue;
                }
                if (foundSearchString)
                {
                    // Start after the divider line
                    if (lastLine.Contains("===================="))
                        continue;

                    // If the current line read is a marker line, then our last line is actually a new identifier line
                    if (line.Contains("===================="))
                    {
                        // Can be used to look for multiple listings with the same ID
                        foundSearchString = false;
                        continue;

                        // If you only want the first found ID, uncomment this and comment out above
                        // return dataEntries.ToArray();
                    }

                    // If our previously read line is not empty, add it to the list of strings
                    if (lastLine.Trim().Length != 0)
                        dataEntries.Add(lastLine);
                }
            }
        }
        return dataEntries.ToArray();

    }

Then to use this function, just call it like so:

string[] entries = GetDataLinesFromFile("input.txt", "Code Name");

To save these to a .csv file, you would just iterate through each of the strings in the string array and look for the tab marker as a divider or if the colums are at a set width then you would hard code those widths in.