Search code examples
c#streamreader

How do I read next x number of lines after finding a line using streamreader


I want to search a text file for a line containing "Performance Stats:" then print the next 20 or so as they are a table of performance stats. I'm using Streamreader.

string pathToFile = "\\host\c$\temp\logs\myPerformanceFile.txt"

using (var filestream = new FileStream(pathToFile, FileMode.Open, FileAccess.Read,                               FileShare.ReadWrite))
using (var streamReader = new StreamReader(filestream, Encoding.Default))
{
    string line;
    while ((line = streamReader.ReadLine()) != null)
    {
        if (line.Contains("Performance Stats:"))
        {
            Console.WriteLine(line)
        }
    }
}

I have tried to find the title column of each of the stats. But it looks a little messy for 20 lines e.g. and there are 5 tables in the file i'm parsing with the same search parms.. but I only want data from one of the tables.

        if (line.Contains("Timings:"))
        {
            Console.WriteLine(line)
        }
        if (line.Contains("Downloads:")) 
        {
            Console.WriteLine(line)
        }
        if (line.Contains("Reset")) 
        {
            Console.WriteLine(line)
        }

Solution

  • You can use a counter. For example:

    int counter = 0; 
    
    while ((line = streamReader.ReadLine()) != null)
    {
        if (line.Contains("Performance Stats:")) // Trigger
        {
            counter = 20; // Number of lines to print
        }
        if (counter > 0) {
            Console.WriteLine(line);
            counter--;
        }
    }
    

    This way you can print the X next lines after your trigger.

    If you put the trigger after writing the line it will skip the one with the condition, of course.