解析日志文件并获取匹配的数据

发布于 2024-09-19 11:09:12 字数 112 浏览 4 评论 0原文

从大日志文件(大约 2532910 行)中,我要查找的行很少(例如 10 或 12 行)。匹配和阅读这些行的最佳方法是什么?我的代码是c#的。有没有办法让读取器/流只能读取模式匹配的数据?

谢谢

From the big log file (about like 2532910 lines), the lines that I am looking for are very few (like 10 or 12). What is the best way to match and read these lines? My code is in c#. Is there a way a reader/stream can read only a pattern matching data?

Thanks

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

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

发布评论

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

评论(1

夏の忆 2024-09-26 11:09:12

要读取这么大的文件,最好的方法是使用streamReader.ReadLine(),

如下所示:

StreamReader sr = new StreamReader(@"path_to_log");

int lineNum = 1;
const int searchingLineNum = 10;
string line = string.Empty;

while (sr.Peek() != -1)
{
    line = sr.ReadLine();

    if (lineNum == searchingLineNum)
    {
        break;
    }
    lineNum++;
}

Console.WriteLine(line); // do what you want with this line (parse using Regex)

to read such a big files the best way is to use streamReader.ReadLine()

just like this:

StreamReader sr = new StreamReader(@"path_to_log");

int lineNum = 1;
const int searchingLineNum = 10;
string line = string.Empty;

while (sr.Peek() != -1)
{
    line = sr.ReadLine();

    if (lineNum == searchingLineNum)
    {
        break;
    }
    lineNum++;
}

Console.WriteLine(line); // do what you want with this line (parse using Regex)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文