0

I have a postscript file that has a starting indicator of where I want to begin copying data to a StringBuilder that reads:

$$StartCopy
$$ChunkID[1234]
$$Type[Foo]
\\bla bla for hundreds or thousands of lines
$$EndCopy  

$$StartCopy
$$ChunkID[4567]
$$Type[Bar]
\\bla bla for hundreds or thousands of lines
$$EndCopy 


$$StartCopy
$$ChunkID[4567]
$$Type[Foo]
\\bla bla for hundreds or thousands of lines
$$EndCopy

$$StartCopy
$$ChunkID[8901]
$$Type[Bar]
\\bla bla for hundreds or thousands of lines
$$EndCopy 

Note that there is no specific line that I can start at, nor is there any set number of lines between $$StartCopy and $$EndCopy. In this case, how do I grab the chunk of text $$Chunk[4567] where $$Type[Bar] from its start to end line?

To be clear, here is what the end result should be:

$$StartCopy
$$ChunkID[4567]
$$Type[Bar]
\\bla bla for hundreds or thousands of lines
$$EndCopy 
Skullomania
  • 2,225
  • 2
  • 29
  • 65
  • What does this mean exactly "*In this case, how do I grab the chunk of text $$Chunk[4567] where $$Type[Bar] from its start to end line*" its not very specific and kind of a bit muddled. whats an end line?. Show the **exact** output you want – TheGeneral Mar 21 '20 at 04:10
  • 1
    BTW, despite making comment it is not @MichaelRandall who downvoted the post for lack of demonstrated research... Showing where you are stuck would make answering much easier than explaining how to read strings from file, compare strings for equality, deal with Boolean and string variables, concatenating strings, basic loops and `break;` statement... – Alexei Levenkov Mar 21 '20 at 04:15
  • @MichaelRandall the End Line is `$$EndCopy` – Skullomania Mar 21 '20 at 04:23
  • @AlexeiLevenkov much like the author of this question https://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c I am also looking for best practice, and a clean solution. I doubt the poster was someone who was new to C# the same in my case. – Skullomania Mar 21 '20 at 04:29

1 Answers1

1

Since you are dealing with a large file and lots of results, it's probably better to use File.ReadLines which returns an IEnumerable<string> and an iterator method

/// <summary>
/// Will return the data from a file between the start and end parameters (inclusive)
/// </summary>
/// <param name="fileName">duh</param>
/// <param name="start">The start of the sequence</param>
/// <param name="end">The end of the sequence</param>
/// <returns>Stuff</returns>
public static IEnumerable<string> GetData(string fileName, string start, string end)
{
   var found = false;
   foreach (var line in File.ReadLines(fileName))
   {
      if (line == start) found = true;

      if (!found) continue;

      yield return line;

      if (line == end) break;
   }
}

Usage

var results = GetData(fileName, "$$StartCopy", "$$EndCopy");

Note : This code is completely untested and void of any warranty claims, take backs, or returns due to the people you may harm or otherwise maim with this code

Great comment by Dai

The problem with NET's built-in ReadLine/ReadLineAsync/ReadLines methods is that they use the Environment.NewLine string and doesn't let you manually specify a line-terminator, making them useless for writing cross-platform code

Which is to say, this method really only deals with the following situations because of the inherent use of Environment.NewLine when determining what an actual line is.

A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • The problem with NET's built-in `ReadLine`/`ReadLineAsync`/`ReadLines` methods is that they use the `Environment.NewLine` string and don't let you manually specify a line-terminator, making them useless for writing cross-platform code. – Dai Mar 21 '20 at 04:55
  • @Dai its worthy of a note, as people have their fingers in also sorts of platforms these days – TheGeneral Mar 21 '20 at 04:56
  • Does it? [StreamReader.ReadLine()](https://referencesource.microsoft.com/#mscorlib/system/io/streamreader.cs,731). `File.ReadLines()` just creates the Iterator, which of course calls `StreamReader.ReadLine()`. – Jimi Mar 21 '20 at 05:55