Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a CSV file one line at a time and parse out keywords

Tags:

c#

I am new to C# and I have started using StreamReader. I am trying to read a file one line at a time and output the line when it matches a specific keyword like "I/RPTGEN".

So far I figured out how to read the entire file into a string, but I'm having trouble figuring out how to just read it one line at a time.

My code so far is this.

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
class Test
{
    public static void Main()
    {
        try
        {
            using (StreamReader sr = new StreamReader("c:/temp/ESMDLOG.csv"))
            {
                String line = sr.ReadToEnd();
                Console.WriteLine(line);

                Console.ReadLine();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The File could not be read:");
            Console.WriteLine(e.Message);

            Console.ReadLine();
        }
    }
}
}

Plus here is a sample of one line in the file.

Advisory,2/27/2013 12:00:44 AM,I/RPTGEN (cadinterface),I/RPTGEN Failed - Error 500 - Internal Server Error - returned for a report request (check log for URL).

like image 608
Steve Avatar asked Mar 21 '13 23:03

Steve


People also ask

How do I read a CSV file by line?

Step 1: Load the CSV file using the open method in a file object. Step 2: Create a reader object with the help of DictReader method using fileobject. This reader object is also known as an iterator can be used to fetch row-wise data. Step 3: Use for loop on reader object to get each row.

What is csv parsing?

The Comma Separated Values (CSV) Parser reads and writes data in a CSV format. Note: In the Config Editor, the parameters are set in the Parser tab of the Connector.


2 Answers

If your CSV file contains just one line the ReadToEnd could be acceptable, but if you have a log file composed of more than one line then it is better to read line by line using ReadLine of the StreamReader object

using (StreamReader sr = new StreamReader("c:/temp/ESMDLOG.csv"))
{
    string currentLine;
    // currentLine will be null when the StreamReader reaches the end of file
    while((currentLine = sr.ReadLine()) != null)
    {
       // Search, case insensitive, if the currentLine contains the searched keyword
       if(currentLine.IndexOf("I/RPTGEN", StringComparison.CurrentCultureIgnoreCase) >= 0)
       {
            Console.WriteLine(currentLine);
       }
    }
}
like image 85
Steve Avatar answered Oct 21 '22 04:10

Steve


Another way to read one line at a time is:

var searchItem = "Error 500";

var lines = File.ReadLines("c:/temp/ESMDLOG.csv");

foreach (string line in lines)
{
    if (line.Contains(searchItem))
    {
        Console.WriteLine(line);
    }
}
like image 28
ClearLogic Avatar answered Oct 21 '22 06:10

ClearLogic