Read specific lines in a Text file between line Contain “Action:---” and line Contain "-----------"

I have a log file with huge number of lines.The problem I need only to read all lines between

line contain("Action:-------------------------")

First Line /* Reading start here*/

Second Line

Third Line

...n Line /* Reading end here*/

and line contain("-------------------------")

Example:

[2014/02/03 00:12:14]<- Info1

[2014/02/03 00:12:16]Info2

[2014/02/03 00:12:35]Info3

[2014/02/03 00:12:40]Info4

[2014/02/03 00:12:41]Action:-------------------------

ActionNumber 12345678981245876

03.02.14 00:12

Value : 20.00 KG

RESPONSE :68

IMPOSSIBLE TO PROCESS

RETRIEVAL REFERENCE NUMBER 12457896


[2014/02/03 00:12:44]Info 1

[2014/02/03 00:12:47]Info 2

[2014/02/03 00:12:47]-> Info 3

[2014/02/03 00:12:55]<- Info 4

[2014/02/03 00:12:56]Info 5

[2014/02/03 00:13:06]Info 6

[2014/02/03 00:13:16]Info 7

[2014/02/03 00:13:17]Action:-------------------------

Action Ref 3692581471245789

03.02.14 00:13

Value : 0.00 KG

RESPONSE :68

IMPOSSIBLE TO PROCESS

RETRIEVAL REFERENCE NUMBER 31285647


[2014/02/03 00:15:01]Info 1

[2014/02/03 00:15:05]Info 2

[2014/02/03 00:15:07]-> Info 3

Here is my code:

static void Main(string[] args)
    {
        var StartReading = "Action:-------------------------";
        var EndReading = "-------------------------";
        string[] items = File.ReadLines("C:\\Users\\Administrator\\Desktop\\LogText.txt")                          
               .SkipWhile(line => !line.Contains(StartReading) )
               .TakeWhile(line => line.Contains(EndReading))
               .ToArray();
        foreach (var item in items)
        {
            Console.WriteLine(item);
        }

        Console.ReadLine();
    }

Comments

  • mazanujmazanuj Ukraine, Kiev

    static void Main(string[] args)
    {
        var StartReading = "Action:-------";
        var EndReading = "-------";
        string[] items = File.ReadAllLines("data.txt");
        bool start = true;  
        bool end = false;
        foreach(string t in items)
        {
            if (t.Contains(StartReading) && start)
            {
                start = false;
                end = true;
                continue;
            }
            else if(end)
            {
                if(t.Contains(EndReading))
                {
                    start = true;
                    end = false;
                    Console.WriteLine(new string('=', 30));
                    continue;
                }
                else Console.WriteLine(t);
            }
        }
        //Delay
        Console.ReadKey();
    }
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion