开发者

C# How to skip String line numbers in Array after processing from Text File?

开发者 https://www.devze.com 2023-01-29 23:26 出处:网络
The program that was created allows users to simply parse a log text file. The program simply utilizes grouping of the various parts of the text files into the variable \"sections\" array.

The program that was created allows users to simply parse a log text file. The program simply utilizes grouping of the various parts of the text files into the variable "sections" array.

However is there a way to skip the number of lines of the "sections" array? I have tried using the "split" method but it does not work as it skips a number of "sections" instead of the number of lines in each "sections".

The lines in each sections should be removed are:

Restore Point Info

Description   : Installed VMware Tools

Type          : Application Install

Creation Time : Mon Nov 29 16:53:12 2010

Therefore may someone please advise on the codes? Thanks!

The codes:

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

            // Skip 5 lines of the original text file
            for(var i = 0; i < 5; i++) { 
                tr.ReadLine();
            }

            // Read the reststring 
            String fullLog = tr.ReadToEnd();
            String[] sections = fullLog.Split(new string[] { SplitBy }, StringSplitOptions.None);
            //String[] lines = sections.Skip(5).ToArray();
            int t = 0;
            // Tried using foreach (String r in sections.skip(4)) but skips sections instead of the Text lines found within each sections
            foreach (String r in sections) {
                Console.WriteLine("The times are : " + t);
                Console.WriteLine(r);
                Console.WriteLine(sections[6]);
Console.WriteLine("============================================================");
                t++;
            }
        }
    }
}

An Example of the Text log file:

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.

----------------------------------------

Restore Point Info
Description   : Installed Hex Workshop v5
Type          : Application Install
Creation Time : Fri Dec  3 04:35:57 2010
J:\syscrawl\Restore\RP10\snapshot\_REGISTRY_MACHINE_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 U开发者_C百科ltra 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 03:03:36 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 03:03:36 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 04:08:49 2010]

----------------------------------------

Restore Point Info

Description   : Installed VMware Tools

Type          : Application Install

Creation Time : Mon Nov 29 16:53:12 2010

J:\syscrawl\Restore\RP2\snapshot\_REGISTRY_MACHINE_SYSTEM

ControlSet001\Enum\USBStor not found.


There are multiple solutions available depending on how maintainable you want to code.

  • Hard-coding the text to remove so that you find and replace it with empty string
  • Read lines one by one and you have a list of all lines to ignore and you check against them
  • Use a regular expression to extract what you need [PREFERRED]

Reality is the log file you are trying to parse does not seem to be generated by your software, i.e. you do not own the format (VMWare does). So I believe this format could be changed by any update so hard-coding the format text you need or you do not need could make your software very brittle.

I would recommend using Regex, perhaps you would spend a while writing the expression but it is clean and useful.


Since the number of lines you want to keep can change, one solution would be to use a token/character at the start of every line you want to remove, that you are sure won't appear on the other log lines. For example:

$Restore Point Info

$Description   : Installed VMware Tools

$Type          : Application Install

$Creation Time : Mon Nov 29 16:53:12 2010

Now you can do:

if(line[0]=="$")
    continue;

EDIT: Since you can only read the file

You could try a dirty way to do it, I think:

bool ShouldSkip(string line)

{
  return (line.StartsWith("Restore Point Info") || line.StartsWith("Description") || line.StartsWith("Type") || line.StartsWith("Creation Time"))
}

usage:

//in your main method
foreach(var line in lines)
{
  if(ShouldSkip(line))
    continue;
}

I don't know if this is what you're looking for.

0

精彩评论

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