0

Both the below cases are not working. I want to extract from a text file a certain part that I can choose by specifying the start of the line and end.

  1. case looks like this:
using (StreamReader reader = new StreamReader("C:/Users/david/Desktop/20180820.log",Encoding.Default))
{
    Console.WriteLine("From:");
    string a = (Console.ReadLine());
    Console.WriteLine(" To:");
    string b = (Console.ReadLine());   
    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        if (line.StartsWith(a) && (line.EndsWith(b)))
        {
            Console.WriteLine(line);
        }
    }
}
  1. case with regex
string line;
while ((line = reader.ReadLine()) != null)
{
    string regex12 = a.ToString() + b.ToString(); 
    Match m = Regex.Match(line,regex12);
    string s = Regex.Match(line, regex12).Groups[0].Value;
    Console.WriteLine(s);
    if (m.Success) 
    {
        string n = m.Groups[0].Value;
        Console.WriteLine(n);
    }
}

If anyone can solve my problem, I will be very thankful.

Paradente
  • 3
  • 4
  • Did you try to debug the code in case 1 with a simple example file (for example only short 2 lines)? Does that work? When you step through your code in the debugger in case 1, is the contents of the variables`a`, `b` and `line` what you expect? – Marius Nov 30 '19 at 12:30
  • [00:09:08.870] text... [00:09:08.886] text... [00:09:08.886] text... [00:09:10.448] text... [00:09:10.464] text... [00:09:10.526] text... [00:09:11.886] text... [00:09:11.901] text... [00:09:11.980] text... [00:09:12.026] text... For Example if i set 00:09:08.870 as a variable a and 00:09:12.026 as a b , i want to get everything from variable a to b including varibles a , b . idk whats wrong with my codes ... – Paradente Dec 01 '19 at 15:50

1 Answers1

0

Update Based on Comments

From the comments, it looks like you are attempting to parse a specifically formatted file, and would better benefit with a non-generic solution. Also, it looks like the text is split across multiple lines, rather than single line as you had shared in comments.

[00:09:08.870] text... 
[00:09:08.886] text... 
[00:09:08.886] text... 
[00:09:10.448] text... 
[00:09:10.464] text... 
[00:09:10.526] text... 
[00:09:11.886] text... 
[00:09:11.901] text... 
[00:09:11.980] text... 
[00:09:12.026] text...

In this case, you could use the following.

var reader = File.OpenText(filePAth);
var startLineDetected = false;
var startWord = "00:09:08.870";
var endWord =  "00:09:12.026";
var strBuilder = new StringBuilder();
while(!reader.EndOfStream)
{
    var newLine = reader.ReadLine();
    if(newLine.Contains($"[{startWord}") && !startLineDetected)
    {
        startLineDetected = true;
    }

    if(newLine.Contains($"[{endWord}") && startLineDetected)
    {
        strBuilder.AppendLine(newLine);
        break;
    }

    if(startLineDetected)
    {
        strBuilder.AppendLine(newLine);
    }
}
var resultData = strBuilder.ToString();

Original Answer based on OP

You could do the following.

var reader = File.OpenText(filePAth);
var startLineDetected = false;
var startWord = // startWord;
var endWord =  // endWord;
var strBuilder = new StringBuilder();
while(!reader.EndOfStream)
{
    var newLine = reader.ReadLine();


    if(newLine.Contains(startWord) && !startLineDetected)
    {
        startLineDetected = true;
        newLine = newLine.Substring(newLine.IndexOf(startWord));
    }

    if(newLine.Contains(endWord) && startLineDetected)
    {
        newLine = newLine.Substring(0,newLine.IndexOf(endWord) + endWord.Length);
        strBuilder.Append(newLine);
        break;
    }


    if(startLineDetected)
    {
        strBuilder.Append(newLine);
    }
}
var resultData = strBuilder.ToString();
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • @David I have updated the answer to include both start and end elements.Please verify – Anu Viswan Dec 01 '19 at 15:55
  • "those two variables" ? Sorry, couldn't understand that. Did you mean the preceding "[" for Start and Succeeding "]" for end ? – Anu Viswan Dec 01 '19 at 18:07
  • Is this a specific file which you are attempting to parse ? All of which have the mentioned format ? And also, are these multiple lines or are they as you have mentioned in comments (single line) ? – Anu Viswan Dec 01 '19 at 18:24
  • @David Could you please check the updated answer. I think I am understanding your text file better now. Please verify the updated answer – Anu Viswan Dec 01 '19 at 18:32