c# - 在每个大小为 150 MB 的多个文本文件中搜索字符串 C#

标签 c# copy

我有多个 150MB 大小的 .txt 文件。使用 C#,我需要从每个文件中检索包含字符串模式的所有行,然后将这些行写入新创建的文件中。

我已经研究过类似的问题,但他们建议的答案都不能为我提供获取结果的最快方法。我尝试了正则表达式、linq 查询、包含方法、字节数组搜索,但所有这些都花费了 30 多分钟来读取和比较文件内容。

我的测试文件没有任何特定的格式,它就像原始数据,我们无法根据分升和基于 DataView 的过滤器进行拆分。以下是该文件中每一行的示例格式。

示例.txt

LTYY;;0,0,;123456789;;;;;;;20121002 02:00;;
ptgh;;0,0,;123456789;;;;;;;20121002 02:00;;
HYTF;;0,0,;846234863;;;;;;;20121002 02:00;;
Multiple records......

我的代码

using (StreamWriter SW = new StreamWriter(newFile))
            {
                using(StreamReader sr = new StreamReader(sourceFilePath))
                {
                while (sr.Peek() >= 0) 
                {
                   if (sr.ReadLine().Contains(stringToSearch))
                     SW.WriteLine(sr.ReadLine().ToString());
                 }
}
}

我想要一个示例代码,只需不到一分钟的时间即可从 Sample.txt 中搜索123456789。如果我的要求不清楚,请告诉我。提前致谢!

编辑

我发现根本原因是文件驻留在远程服务器中,这会消耗更多时间来读取它们,因为当我将文件复制到本地计算机中时,所有比较方法都很快完成,因此这不是问题我们阅读或比较内容的方式,或多或少花费了相同的时间。

但是现在我该如何解决这个问题,我无法将所有这些文件复制到我的计算机上进行比较并获得 OutOfMemory 异常

最佳答案

最快的搜索方法是使用 Boyer–Moore string search algorithm因为此方法不需要从文件中读取所有字节,但需要随机访问字节,您可以尝试使用 Rabin Karp Algorithm

或者您可以尝试执行类似以下代码的操作,来自 this answer :

  public static int FindInFile(string fileName, string value)
  {   // returns complement of number of characters in file if not found
    // else returns index where value found
  int index = 0;
   using (System.IO.StreamReader reader = new System.IO.StreamReader(fileName))
   {
    if (String.IsNullOrEmpty(value))
        return 0;
    StringSearch valueSearch = new StringSearch(value);
    int readChar;
    while ((readChar = reader.Read()) >= 0)
    {
        ++index;
        if (valueSearch.Found(readChar))
            return index - value.Length;
    }
}
return ~index;
}
 public class StringSearch
 {   // Call Found one character at a time until string found
private readonly string value;
private readonly List<int> indexList = new List<int>();
public StringSearch(string value)
{
    this.value = value;
}
public bool Found(int nextChar)
{
    for (int index = 0; index < indexList.Count; )
    {
        int valueIndex = indexList[index];
        if (value[valueIndex] == nextChar)
        {
            ++valueIndex;
            if (valueIndex == value.Length)
            {
                indexList[index] = indexList[indexList.Count - 1];
                indexList.RemoveAt(indexList.Count - 1);
                return true;
            }
            else
            {
                indexList[index] = valueIndex;
                ++index;
            }
        }
        else
        {   // next char does not match
            indexList[index] = indexList[indexList.Count - 1];
            indexList.RemoveAt(indexList.Count - 1);
        }
    }
    if (value[0] == nextChar)
    {
        if (value.Length == 1)
            return true;
        indexList.Add(1);
    }
    return false;
}
public void Reset()
{
    indexList.Clear();
}
}

关于c# - 在每个大小为 150 MB 的多个文本文件中搜索字符串 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13570528/

相关文章:

c++ - C/C++如何复制结构?

mysql - 使用另一个表中的值更新表新列 (MySQL)

在谷歌大查询中将表从一个数据集复制到另一个数据集

c# - 序列化包含 XDocument 的属性

javascript - 如何自动将文本附加到使用 JavaScript 复制的文本

Python:递归复制文件夹内容

c# - 手动锁定和同步方法之间的区别

c# - 数组的第二个元素发生了什么?

c# - AutoMapper.AutoMapperMappingException异常

c# - 检查对象是否为数字