开发者

C# How to skip lines in Text file after text converted to array?

开发者 https://www.devze.com 2023-01-29 21:17 出处:网络
I have a program which processes a text log file and tokenizes the text files. After tokenizing the text file, the program should be able to skip the first 5 lines of the new output which contains

I have a program which processes a text log file and tokenizes the text files.

After tokenizing the text file, the program should be able to skip the first 5 lines of the new output which contains

"RipXP v.20081001

Launched Fri Dec 3 12:50:21 2010 Z

J:\syscrawl\registry\config\system

USBStor

ControlSet001\Enum\USBStor"

Can someone please advise on the codes?

The Codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;


namespace Testing
{
class Program
{
    static void Main(string[] args)
    {
        TextReader tr = new StreamReader(@"C:\Test\new.txt");

        String SplitBy = "----------------------------------------";

        String fullLog = tr.ReadToEnd();

        String[] sections = fullLog.Split(new string[] { SplitBy }, StringSplitOptions.None);

        foreach (String r in sections)
        {
            Console.WriteLine(r);
   开发者_JS百科         Console.WriteLine("============================================================");
        }
    }
  }
  }

An Sample of the output:

"RipXP v.20081001 Launched Fri Dec 3 12:50:21 2010 Z

J:\syscrawl\registry\config\system USBStor ControlSet001\Enum\USBStor

CdRom&Ven_SanDisk&Prod_Ultra_Backup&Rev_8.32 [Wed Dec 1 07:39:09 2010 S/N: 2584820A2890B317&1 [Wed Dec 1 07:39:22 2010] FriendlyName : SanDisk Ultra Backup USB Device

CdRom&Ven_WD&Prod_Virtual_CD_070A&Rev_1032 [Wed Dec 1 07:31:33 2010] S/N: 575836314331304639303339&1 [Fri Dec 3 05:41:48 2010] FriendlyName : WD Virtual CD 070A USB Device

Disk&Ven_SanDisk&Prod_Ultra_Backup&Rev_8.32 [Wed Dec 1 07:39:09 2010] S/N: 2584820A2890B317&0 [Wed Dec 1 07:39:19 2010] FriendlyName : SanDisk Ultra Backup USB Device ParentIdPrefix: 8&2f23e350&0

Disk&Ven_WD&Prod_My_Passport_070A&Rev_1032 [Wed Dec 1 07:31:33 2010] S/N: 575836314331304639303339&0 [Fri Dec 3 05:41:48 2010] FriendlyName : WD My Passport 070A USB Device

Other&Ven_WD&Prod_SES_Device&Rev_1032 [Wed Dec 1 07:31:33 2010] S/N: 575836314331304639303339&2 [Fri Dec 3 05:41:48 2010]

============================================================

Restore Point Info Description : System Checkpoint Type : System Checkpoint Creation Time : Mon Nov 29 16:51:52 2010

J:\syscrawl\Restore\RP1\snapshot_REGISTRY_MACHINE_SYSTEM

ControlSet001\Enum\USBStor not found.

============================================================"


ooooh i think u want to concat string from original

String.Replace is best solution here

string data = tr.ReadToEnd();
data.Replace(Environment.NewLine, " ")
.Replace("----------------------------------------", "============================================================");
Console.WriteLine(data);

edit again

sorry, i really dont understand the question. but to skip first 5 lines

string lines = tr.ReadToEnd().Split('\n');
StringBuilder sb = new StringBuilder();
for(int i = 5; i < lines.Length; i++) sb.AppendLine(lines[i]);
string sixthLineToEnd = sb.ToString();

hope it help


The answer was the Stream Reader which should start reading the various line position before using the split method. The codes: C# How to skip number of lines while reading text file using Stream Reader?

0

精彩评论

暂无评论...
验证码 换一张
取 消