0

我正在尝试解析 C# 中的文本文件。我想提取“Rows_affected”值大于 500 的行号。

这是我的文本文件的示例..

Query_time: 0.000268  Lock_time: 0.000097  Rows_sent: 1  Rows_examined: 38  Rows_affected: 0    Rows_read: 1
Query_time: 0.000308  Lock_time: 0.000115  Rows_sent: 0  Rows_examined: 38  Rows_affected: 500  Rows_read: 0
Query_time: 0.000169  Lock_time: 0.000057  Rows_sent: 0  Rows_examined: 38  Rows_affected: 300  Rows_read: 0
Query_time: 0.000296  Lock_time: 0.000111  Rows_sent: 0  Rows_examined: 38  Rows_affected: 50   Rows_read: 0
Query_time: 0.000238  Lock_time: 0.000081  Rows_sent: 0  Rows_examined: 38  Rows_affected: 1    Rows_read: 0
Query_time: 0.000318  Lock_time: 0.000110  Rows_sent: 1  Rows_examined: 38  Rows_affected: 600  Rows_read: 1

这是我的代码...

int lineNumb = 0;
using (System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName)) {
    while ((line = file.ReadLine()) != null) {
        lineNumb++;
        if (line.StartsWith("# Query_time:")) {
            int value = Convert.ToInt32(line.Split(':')[5].Split(' ')[1]);
            if (value > 500) {
                listBox1.Items.Add(lineNumb.ToString() + " > " + value.ToString()); 
            }

int lineNumb = 0;
        using (System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName))
        {
            while ((line = file.ReadLine()) != null)
            {
                lineNumb++;
                if (line.StartsWith("# Query_time:"))
                {
                    int value = Convert.ToInt32(line.Split(':')[5].Split(' ')[1]);
                    if (value > 500)
                    {
                        listBox1.Items.Add(lineNumb.ToString() + " > "  + value.ToString());
                    }

                }

            }
4

4 回答 4

3

使用 linq 然后类似

static void Main()
{
    var lines = ReadAllLines(@"path\to\your\file.txt");

    var lineNumbers = lines.Where(l => Criteria(l))
                           .Select((s, i) => i);

    foreach (var i in lineNumbers)
    {
        Console.WriteLine("Criteria met on line " + i);
    }
}

static bool Criteria(string s)
{
    int i = int.Parse(Regex.Match(s, "(?<=Rows_affected: )\d+").Value);

    return i > 500;
}
于 2013-06-28T11:11:01.010 回答
1

这里我有一个简单的解决方案,即逐行读取 ' ' 和 ' ' 然后拆分行,该值将位于拆分字符串数组中的第 9 个索引处,简单地将其转换为 int 并与值 500 进行比较。

如果它更大,它将返回 true,您会看到,行号打印到控制台窗口。

以下是保存在文件中的数据:

Query_time: 0.000268  Lock_time: 0.000097  Rows_sent: 1  Rows_examined: 38  Rows_affected: 0    Rows_read: 1
Query_time: 0.000308  Lock_time: 0.000115  Rows_sent: 0  Rows_examined: 38  Rows_affected: 500  Rows_read: 0
Query_time: 0.000169  Lock_time: 0.000057  Rows_sent: 0  Rows_examined: 38  Rows_affected: 300  Rows_read: 0
Query_time: 0.000296  Lock_time: 0.000111  Rows_sent: 0  Rows_examined: 38  Rows_affected: 50   Rows_read: 0
Query_time: 0.000238  Lock_time: 0.000081  Rows_sent: 0  Rows_examined: 38  Rows_affected: 1    Rows_read: 0
Query_time: 0.000318  Lock_time: 0.000110  Rows_sent: 1  Rows_examined: 38  Rows_affected: 600  Rows_read: 1

//这里是实际代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LineNumber
{
    class Program
    {
        static System.IO.StreamReader sr;
        static int linenumber = 1;
        static string line;
        static void Main(string[] args)
        {
            try
           {
                sr = new StreamReader("data.dat");
                while ((line = sr.ReadLine()) != null)
                {
                    if (FindNumber(line) == true)
                    {
                        Console.WriteLine("Line Number : " + linenumber++);
                    }
                    else
                        linenumber++;
                }
                Console.WriteLine("End of File");
                Console.ReadLine();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        static bool FindNumber(string line)
        {
            string[] linesplit;
            try
            {
                if (line.Length == 0)
                    return false;
                else
                {
                    line = line.Replace("  "," ");
               }
                linesplit = line.Split(' ');
                if(int.Parse(linesplit[9]) > 500)
                    return true;
                else
                    return false;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return false;
        }
    }
}

截屏: 在此处输入图像描述

于 2013-06-28T11:46:47.797 回答
1

在每一行上使用正则表达式来识别受影响的行。然后使用整数值来标识行号。

string filename = "yourfilename";
Regex r = new Regex(@"(?<rowsaffected>(?<=Rows_affected:\s)[0-9]*)");
int lineNumber = 1;            
using(TextReader reader = new StreamReader(filename))
{
    string line = reader.ReadLine();
    while(line != null)
    {
        Match m = r.Match(line);
        int rowsaffected = int.Parse(m.Groups["rowsaffected"].Value);
        if(rowsaffected > 500)
        {                        
            // do whatever you want with your linenumber here.
        }
        lineNumber++;
        line = reader.ReadLine();
    }
}
于 2013-06-28T11:01:57.193 回答
0

以面值的文件格式为例,一个简单的选择是在每一行上搜索“Rows_affected:”字符串,然后解析它后面的文本。

        string row = <<row from your file>>                

        int startPosition = row.IndexOf("Rows_affected: ") + "Rows_affected: ".Length;
        int endPosition = row.IndexOf(" ", startPosition);

        int numberAffected = Int32.Parse(row.Substring(startPosition, endPosition - startPosition));

然后,您可以检查 numberAffected 并根据需要记录下来。为简洁起见省略了错误检查。

于 2013-06-28T11:11:18.780 回答