20

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).

NotMe
  • 87,343
  • 27
  • 171
  • 245
Steve
  • 223
  • 1
  • 2
  • 4
  • 5
    @Tosi His question was in the title "How to read a CSV file one line at a time and parse out keywords". Don't be so harsh. – Dustin Davis Mar 21 '13 at 23:40

2 Answers2

43

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);
       }
    }
}
Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
Steve
  • 213,761
  • 22
  • 232
  • 286
  • THank you all for the answers. It has allowed me to gt closer to finishing my project. – Steve Mar 26 '13 at 19:02
  • What if I was looking for multiple keywords like Date and Serial Number and also, what if in my CSV there are multiple keywords that contain Date e.g. ExpirationDate vs. just Date, how do I control which keywords to look for? Thank you so much! – Kala J May 16 '14 at 16:40
  • Sorry, but with the current info I cannot answer. What about a new question where you specify all the requirements?. More people than just me will look at your problem. Of course you could add a link back to this question and, notify me with another link to the new question here. – Steve May 16 '14 at 16:57
  • 1
    Okay sure, thank you. Hope this helps clarify things better: http://stackoverflow.com/questions/23704320/how-can-i-parse-csv-line-by-line-and-parse-out-multiple-keywords-and-their-data – Kala J May 16 '14 at 20:49
  • 1
    This will not work if the CSV has linefeeds inside the actual columns (Encapsulated with quotes) – Sal Dec 02 '16 at 18:39
12

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);
    }
}
Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
ClearLogic
  • 3,616
  • 1
  • 23
  • 31