【问题标题】:Extract strings from file and save in another using c#从文件中提取字符串并使用c#保存在另一个文件中
【发布时间】:2012-07-04 08:13:43
【问题描述】:

我有 300 000 行的 .txt 文件。有没有办法从该文件中提取特定的字符串(行)并保存在另一个 .txt 或 excel 中仅提取的行? 我谈论日志文件,我在其中保存一些请求,每个请求都花费时间。我想做的是只提取每个请求的时间,然后我会计算平均花费的时间。

希望你们明白我在说什么。

编辑: .txt 文件的格式是纯文本,每个请求都以。所以我有:

Starting date
//body of response from server
End date
Time: 3150,0792 ms <--- time taken

所以,我有 10 000 个请求和 10 000 个响应。我只需要提取每个时间,因为手动滚动整个 .txt 文件并检查每个时间将花费我很多时间。

【问题讨论】:

  • 几行你的 txt 文件怎么样,这样我们就可以看到格式,然后是你希望提取的文本文件看起来像的示例。
  • What have you tried?在循环中使用 StreamReader.ReadLine() 就可以了。
  • @CodeCaster,如果他有 300 000 行,那不是一个好主意。他最好一次读几行。
  • 当然,你试过什么?正则表达式可能是一个很好的起点msdn.microsoft.com/en-us/library/…
  • @CodeCaster 这并不是故事的全部。鉴于File.ReadLines 的存在,我认为没有必要使用File.ReadAllLines。再说了,这不就是一个读写CSV类型文件的问题吗?

标签: c# string file


【解决方案1】:

你可以通过文件类来实现

using (StreamWriter sw = File.AppendText("File2.txt")) 
{
    foreach (string line in File.ReadLines(@"d:\File1.txt"))
    {
        if (line.Contains("TheWordInLine"))//This is the line you want by matching something
        {
                sw.WriteLine("line);
        }
    }
}

【讨论】:

  • 在循环外部的范围内只创建一次StreamWriter不是更好吗?
  • 好的,但为什么我进入文件Time: ?应该是Time: Something ms
【解决方案2】:

您可以尝试结合使用MemoryMappedFileTextReader。 MMF 允许您访问大文件,文本阅读器允许您以逐行方式处理文件。

using (var mmf = 
            MemoryMappedFile.CreateFromFile(@"c:\large.data", FileMode.Open
{
    using (MemoryMappedViewStream stream = mmf.CreateViewStream())
    {
        TextReader tr = new StreamReader(stream);
        while ((line = sr.ReadLine()) != null) 
        {
            Console.WriteLine(line);
        }
    }
}

【讨论】:

    【解决方案3】:
        private void extract_lines(string filein, string fileout)
        {
            using (StreamReader reader = new StreamReader(filein))
            {
                using (StreamWriter writer = new StreamWriter(fileout))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (line.Contains("what you looking for"))
                        {
                            writer.Write(line);
                        }
                    }
                }
            }
        }
    

    【讨论】:

    • 实际上这提取了我需要的一切。
    【解决方案4】:

    当然,你可以使用StreamReader/StreamWriter

    using (var input = File.OpenText("input.log"))
    using (var output = File.CreateText("output.log"))
    {
        string line;
        while ((line = input.ReadLine()) != null)
        {
            if (SomeConditionOnLine(line))
            {
                output.WriteLine(line);
            }
        }
    }
    

    这将逐行读取输入文件,因此一次在内存中只有一行,如果该行满足您正在寻找的某些条件,则将其写入输出文件。它会很快并且消耗很少的内存,并且适用于巨大的输入文件。

    【讨论】:

    • 这项工作也可以,但只写Time。应该是Time: time ms。如何花时间
    【解决方案5】:

    正如其他人已经说过的,提供格式示例会很有用。 无论如何,也许你会发现这个工具很有用:

    http://filehelpers.sourceforge.net/

    我在工作中使用它,它允许您解析和写入不同的文件格式。 希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多