Endovelicvs
Endovelicvs

Reputation: 119

Read a line between specific lines

I have a text file structured this way (part of the file):

          B                            NO        AMOUNT
            W                          10        200.00
            R                           1        110.00
            TOTAL                      11         90.00
                    111 TOTALS
          A                            NO        AMOUNT
            W                       4,159        640.00
            R                           0         00.00
            TOTAL                   4,159        640.00

          B                            NO        AMOUNT
            W                          10        200.00
            R                           1        110.00
            TOTAL                      11         90.00

                    222  TOTALS
          A                            NO        AMOUNT
            W                       4,000        510.00
            R                           0         10.00
            TOTAL                   4,000        500.00

And I need to read the lines that start with "TOTAL" between the lines "111 TOTALS" and "222 TOTALS".

I have the following code to read the lines started with "TOTAL", I am just lost on how / where in the code can I include this "between" condition:

using System;
using System.IO;

public class TextFile4
{
  public static void ReadTextFile4()
  {
    int counter = 0;
    string line;

    //Find application path
    string dir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

    //Name of the file
    string filepath = dir + @"\Reports\abc.txt";

    // Read the file and display it line by line
    StreamReader file = new StreamReader(filepath);

    // Loop through text file
    while ((line = file.ReadLine()) != null)
    {
        if (line.Trim().StartsWith("TOTAL"))
        {
            Console.WriteLine(line);
        }
    counter++;
    }
    file.Close();
  }
}

Probably searched for the wrong thing, but couldn't find a similar topic. Saw some topics about counting the nr of lines, but its a variable in this file, so not an option.

Upvotes: 1

Views: 304

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460288

Here is the obligatory LINQ approach which should work (untested):

string[] totalLines = File.ReadLines(filepath)
    .SkipWhile(l => !l.TrimStart().StartsWith("111 TOTALS"))
    .TakeWhile(l => !l.TrimStart().StartsWith("222  TOTALS"))
    .Where(l => l.TrimStart().StartsWith("TOTAL"))
    .ToArray();

Result:

        TOTAL                   4,159        640.00
        TOTAL                      11         90.00

Upvotes: 2

WeSt
WeSt

Reputation: 2684

Just read lines until you encounter the first condition and stop when encountering the second. Example:

bool startFound= false;
while ((line = file.ReadLine()) != null)
    {
        var content = line.Trim();
        if (content.StartsWith("111 TOTALS")) 
        {
            startFound= true;
        }
        if (content.StartsWith("TOTAL") && startFound)
        {
            // this code is only called when the "111 TOTALS" was found
            // and the line starts with "TOTAL"
            Console.WriteLine(line);
        }
        if (content.StartsWith("222 TOTALS"))
        {
            // this will end the loop
            break;
        }
        counter++;
    }

Furthermore, it is a good idea to use the using statement when dealing with streams. See this article here on how to use it.

Upvotes: 2

Related Questions