C# 删除包含字符串的行并更改左行的值

C# 删除包含字符串的行并更改左行的值,c#,regex,C#,Regex,我需要一些帮助,从包含指定字符串的多个文本文件(50.000)中删除一些行,然后更改左行的值,在我的情况下,可以是0行或任意随机数。 下面是一个示例代码: This is a test to remove lines containing a string: //some other lines 0 0 //... 7 This line contains string "ONE" This line contains string "2" This l

我需要一些帮助,从包含指定字符串的多个文本文件(50.000)中删除一些行,然后更改左行的值,在我的情况下,可以是0行或任意随机数。 下面是一个示例代码:

This is a test to remove lines containing a string:
//some other lines
0
0
//...

7
This line contains string "ONE"
This line contains string "2"
This line contains string "THREE", "9"
This line contains string "FOUR"
This line contains string "5"
This line contains string "SIX" , "ZERO"
This line contains string "SEVEN"
//more consecutive lines and 7 value from top, represents the number of lines that are following, until other function starts.

0
0
//...
这是C#中的一个函数,我使用它删除行:

foreach (var f in Directory.GetFiles(@"E:/WORK/Tests", "*", SearchOption.AllDirectories).AsParallel())
{
    string[] LinesToRemove = { "ONE", "2", "THREE", "FOUR", "5", "SIX", "SEVEN" };
    var query = File.ReadAllLines(f, Encoding.Unicode).Where(line => !LinesToRemove.Any(line.Contains));
    File.WriteAllLines(f, query, Encoding.Unicode);
}
7,是行数。 例如,我想删除所有7行(使用stringlineremove)并向上移动1行,然后用0替换这7行。 在该值7之后,所有行都是连续的。

嗯,这很有趣

给你

foreach (var f in Directory.GetFiles(@"E:/WORK/Tests", "*", SearchOption.AllDirectories).AsParallel())
{
    string[] LinesToRemove = { "ONE", "2", "THREE", "FOUR", "5", "SIX", "SEVEN" };
    string[] linesInFile = File.ReadAllLines(f);
    List<string> newFileLines = new List<string>();

    for (int i = 0; i < linesInFile.Length;)
    {
        int n;
        bool isNumeric = int.TryParse(linesInFile[i], out n);
        if (isNumeric && n > 0)
        {
            List<string> tempLines = new List<string>();
            int remainingLines = 0;
            int m = i + n + 1;
            for (int y = i + 1; y < m; y++)
            {
                if(y >= linesInFile.Length)
                {
                    break; //I think this should work since i haven't checked
                    // Or something like i++; break;

                }

                if (!LinesToRemove.Any(s => linesInFile[y].Contains(s)))
                {
                    tempLines.Add(linesInFile[y]);
                    remainingLines++;
                }
                i++;
            }
            i++;
            newFileLines.Add(remainingLines.ToString());
            newFileLines.AddRange(tempLines);
        }
        else
        {
            newFileLines.Add(linesInFile[i]);
            i++;
        }
    }
    File.WriteAllLines(f, newFileLines);
}
foreach(目录.GetFiles(@“E:/WORK/Tests”、“*”、SearchOption.AllDirectories.aspallel()中的变量f)
{
字符串[]LinesToRemove={“一”、“二”、“三”、“四”、“五”、“六”、“七”};
string[]linesInFile=File.ReadAllLines(f);
List newFileLines=newlist();
对于(int i=0;i0)
{
列表模板=新列表();
整数剩余线=0;
int m=i+n+1;
对于(int y=i+1;y=linesInFile.Length)
{
break;//我想这应该行得通,因为我还没检查过
//或者像i++;break;
}
如果(!LinesToRemove.Any(s=>linesInFile[y]。包含(s)))
{
tempLines.Add(linesInFile[y]);
剩余行++;
}
i++;
}
i++;
添加(remainingLines.ToString());
newFileLines.AddRange(模板行);
}
其他的
{
添加(linesInFile[i]);
i++;
}
}
File.writeAllines(f,新文件行);
}

“删除包含这些字符串的行”,然后具体执行什么操作?那部分根本不清楚。请详细说明。删除所有这7行,7必须为0。或者只删除3行,第7行的数量变为4行。但是现在我需要删除所有7行,并用0来更改(替换)7。那么,您是提供数字(7或3)作为输入参数,还是根据某个参数来确定?此外,您的代码不会检查删除的行是否连续。它们都是连续的还是以块的形式存在?他们的计数总是在前一行吗?请回答问题,清楚地描述你想要达到的目标。7代表7之后的行数,是的,行总是连续的。一个程序读取该7个值并期望执行7行。我需要删除行并用0替换7。基本上,在删除所有七行之后,向上移动一行并用0替换7。使用正则表达式,我可以用0替换所有内容,包括7值。但是非常慢而且错误,每读一行就写一个0。这很有效,而且非常快:)。我现在想学习你的代码。谢谢尝试处理许多文件时,我遇到一个错误:“System.IndexOutoforangeException HResult=0x80131508 Message=Index超出了数组的边界。”在这一行:``如果(!LinesToRemove.Any(s=>linesInFile[y].Contains(s))``将其添加到所有内容之外
string currentFile=”“和
int currentLine=0
,然后在
foreach file`add
currentFile=f
开始时,在每个
i++
add
currentLine=i
之前,也在
try/catch
内包装整个foreach循环(不要包装
currentFile
)。在
catch
中添加类似
var asd=currentFile的内容;var ddd=currentLine
之后,当出现错误时,您将在发生错误的文件中找到错误所在的行。把那个文件给我,这样我就可以看到哪里出了问题(或者你自己看)@noaddition哦,我知道哪里出了问题。例如,如果文件有两行,如
3
第二行
我的代码将尝试刺激3行,因为我没有检查该故障。。。现在检查编辑3分钟!我刚做了55.000个文件:)如果我遇到任何其他问题,我会尝试自己解决,不想太麻烦你。再次感谢!