3

I have a text file in which a particular character repeats at the start of the line after every few lines. the no. of lines in between is not fixed. I am able to find out those lines where this condition occurs. I want to read those lines in between.

 using (StreamReader sr = new StreamReader(@"text file"))
 {
     string line;
     while ((line = sr.ReadLine()) != null)
     {
         if (line.StartsWith("some character"))

Because the next time, this character occurs, the code remains same. I am not able to read those lines in between

For eg.

Condition at the begining of a line
Next line
Next line
Condition at the begining of a line
Next Line
Next Line
Next Line
Next Line
Condition at the begining of a line

I have to read lines in between. The condition remains same every time. Thanks.

Christophe Geers
  • 8,564
  • 3
  • 37
  • 53
Sumit
  • 277
  • 1
  • 5
  • 19
  • 2
    I can think of a couple of approaches to this. What have you tried so far? – David Jun 25 '12 at 19:17
  • What do you want to do with the lines you read? To store them in a list without any relation with the condition (=strip the lines that begins with the condition)? To group them in a map (=each condition is the key and each group of lines for that condition is the list for the key)? – Adriano Repetti Jun 25 '12 at 19:20

6 Answers6

4

I assume you want to parse the text file and process all the blocks of text between conditions each time.

Basically you read each line, check for the first condition, which signals the start of a "block" and keep reading the lines until you find another condition, which signals the end of the block (and the start of another).

Wash, rinse, repeat.

A simple example:

using (var reader = new StreamReader(@"test.txt"))
{
    var textInBetween = new List<string>();

    bool startTagFound = false;

    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        if (String.IsNullOrEmpty(line))
            continue;

        if (!startTagFound)
        {
            startTagFound = line.StartsWith("Condition");
            continue;
        }

        bool endTagFound = line.StartsWith("Condition");
        if (endTagFound)
        {
            // Do stuff with the text you've read in between here
            // ...

            textInBetween.Clear();
            continue;
        }

        textInBetween.Add(line);
    }
}
Christophe Geers
  • 8,564
  • 3
  • 37
  • 53
  • 1
    Thx.you got it absolutely exact what i wanted. Its working for me. The beauty is with the continue keyword, the continue keyword allows you to skip the execution of the rest of the iteration. It jumps immediately to the next iteration in the loop. I made an array to hold the data in between, indexed with count, concatenating the datafound in between to this variable with a count initialised to zero once endtagfound. I was not able to use textinbetween to concatenate data each time it is found. as i want to write data found in between in a single line to a new file. – Sumit Jun 27 '12 at 00:04
4

It seems you only have to skip lines that meet a certain consdition.

var lines = System.IO.File.ReadLines(@"text file")
            .Where (line => ! line.StartsWith("Condition");

foreach(string line in lines)
{
  // do your stuff
}
H H
  • 263,252
  • 30
  • 330
  • 514
1

If I have understood correctly, you want to toggle between the condition "inside or outside a block":

{
  string line;
  int inside = 0;
  while ((line = sr.ReadLine()) != null)
  {
     if (line.StartsWith("some character"))
     {
         inside = !inside;
         // If you want to process the control line, do it here.
         continue;
     }
     if (inside)
     {
         // "line" is inside the block. The line starting with "some character"
         // is never here.
     }
     else
     {
         // Well, line is outside. And the control lines aren't here either.
     }
  }
}
LSerni
  • 55,617
  • 10
  • 65
  • 107
1

This is a method I use pretty often for reading in xml files too large for XDocument. It would be pretty easy to modify it for your needs.

public static void ReadThroghFile(string filePath, int beingLinesToSkip, string endingMarker, Action<string> action)
{
   using (var feed = new StreamReader(filePath))
   {
       var currentLine = String.Empty;
       var ongoingStringFromFeed = String.Empty;

       for (var i = 0; i < beingLinesToSkip; i++) feed.ReadLine(); 

       while ((currentLine = feed.ReadLine()) != null)
       {
           ongoingStringFromFeed += currentLine.Trim();
           if (!currentLine.Contains(endingMarker)) continue;

           action(ongoingStringFromFeed);
           ongoingStringFromFeed = String.Empty;
       }
    }
}
amsprich
  • 201
  • 1
  • 8
0

Unless I am missing something, it seems like you are reading "every line" from the StreamReader and checking if your 'Condition' is satisfied. So I didn't understand your question "I want to read those lines in between. "!!!!

usp
  • 797
  • 3
  • 10
  • 24
  • 1
    Yeah, I know!!! but for some reason, I cannot post comments on others questions or answers!! – usp Jun 25 '12 at 19:32
  • 1
    @Uday Then you should simply wait until you have enough reputation to post comments, rather than posting an answer that's not an answer. – Servy Jun 25 '12 at 19:41
  • Seriously... '-1' for asking for more information so that I can answer the question??? Hmm, or it is just because I don't know how to post "comments"...lol, anyways I don't want to do research on 'How to post comments on Stack Overflow' [i checked FAQ]...although if somebody can tell me how to do it, i will definitely do that. Guess, until I figure that out, I should not post anything except my own questions :) – usp Jun 25 '12 at 19:49
0

Couldn't you just else your if to do what you want?

using (StreamReader sr = new StreamReader(@"text file"))
{
 string line;
 while ((line = sr.ReadLine()) != null)
 {
     if (line.StartsWith("some character"))
     {
        //Get rid of line
     }
     else
     {
       // Do stuff with the lines you want
     }  
JohnP
  • 402
  • 1
  • 8
  • 25
  • i need to store data from the lines in between into some temp string variable. all this data is unique every time it is read in between the condition encountered. In a sense, first time condition found , serach till next time it is found and then store all lines in between in temp variable for processing. – Sumit Jun 26 '12 at 02:12
  • 1
    So, create some sort of holding mechanism, datatable, array, whatever. Search for flag variable, then start adding to storage until you hit next flag. The frame code is there, you just need to add the processing. – JohnP Jun 26 '12 at 02:20