C# 如何在文本转换为数组后跳过文本文件中的行?

发布于 2024-10-07 11:14:59 字数 2448 浏览 0 评论 0原文

我有一个程序可以处理文本日志文件并对文本文件进行标记。

对文本文件进行标记后,程序应该能够跳过新输出的前 5 行,其中包含

“RipXP v.20081001

Launched Fri Dec 3 12:50:21 2010 Z

J:\syscrawl\registry\config\system

USBStor

ControlSet001\Enum\USBStor"

有人可以建议一下代码吗?

代码:

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);
            Console.WriteLine("============================================================");
        }
    }
  }
  }

输出示例:

“RipXP v.20081001 于 2010 年 12 月 3 日星期五 12:50:21 启动 Z

J:\syscrawl\registry\config\system USB存储器 ControlSet001\Enum\USBStor

CdRom&Ven_SanDisk&Prod_Ultra_Backup&Rev_8.32 [2010 年 12 月 1 日星期三 07:39:09] 序列号:2584820A2890B317&1 [2010 年 12 月 1 日星期三 07:39:22] 友好名称:SanDisk Ultra 备份 USB 设备

CdRom&Ven_WD&Prod_Virtual_CD_070A&Rev_1032 [2010 年 12 月 1 日星期三 07:31:33] 序列号:575836314331304639303339&1 [2010 年 12 月 3 日星期五 05:41:48] 友好名称:WD 虚拟 CD 070A USB 设备

磁盘&Ven_SanDisk&Prod_Ultra_Backup&Rev_8.32 [星期三 12 月 1 07:39:09 2010] 序列号:2584820A2890B317&0 [2010 年 12 月 1 日星期三 07:39:19] 友好名称:SanDisk Ultra 备份 USB 设备 ParentIdPrefix:8&2f23e350&0

Disk&Ven_WD&Prod_My_Passport_070A&Rev_1032 [2010 年 12 月 1 日星期三 07:31:33] 序列号:575836314331304639303339&0 [2010 年 12 月 3 日星期五 05:41:48] 友好名称:WD My Passport 070A USB 设备

其他&Ven_WD&Prod_SES_Device&Rev_1032 [2010 年 12 月 1 日星期三 07:31:33] 序列号:575836314331304639303339&2 [2010 年 12 月 3 日星期五 05:41:48]

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

还原点信息 描述:系统检查点 类型:系统检查点 创建时间:Mon Nov 29 16:51:52 2010

J:\syscrawl\Restore\RP1\snapshot_REGISTRY_MACHINE_SYSTEM

ControlSet001\Enum\USBStor 未找到。

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

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);
            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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青巷忧颜 2024-10-14 11:14:59

哦,我想你想从原始

字符串中连接字符串。替换是最好的解决方案,在这里

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

再次编辑

抱歉,我真的不明白这个问题。但跳过前 5 行

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();

希望有帮助

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

橙幽之幻 2024-10-14 11:14:59

答案是流读取器,它应该在使用 split 方法之前开始读取各个行位置。代码: C# 如何在使用 Stream Reader 读取文本文件时跳过行数?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文