从文本文件中查找行,然后使用c#将其复制到新的文本文件中

时间:2018-11-01 07:34:28

标签: c# file-io

我有一个日志文本文件,我想对其进行总结。下面是我的日志文本文件示例。

Date        |ID    |Folder               |Doc   |FileName   |NoOfAcct| 
20181024    |1     |ABC.20181101.CGLOG   |test  |xxxx       |12
20181024    |2     |CDE.20181013.CGLOG   |home  |XyyX       |2
20181024    |3     |WQE.20181013.CGLOG   |office|xy         |5
20181024    |4     |ABC.20181013.CGLOG   |store |yy         |10

我想对其进行总结,并用FolderID将它分开 group )。例如,我要将具有Folder 1 ID ABC.20181101.CGLOG 复制到新文本文件 。我确实编写了一些代码,但是我被卡住了。

using System;
using System.IO;

namespace HLB
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            string path1 = "";                
            if (path1 == "")
                path1 = "F:\\Work\\FLP Code\\test\\log_test.txt";            
            string[] strArray1 = File.ReadAllLines(path1);
            string fileName = Path.GetFileName(path1);
            string directoryName = Path.GetDirectoryName(path1);
            if (fileName.Contains("%ABC%"))
            {
                Console.WriteLine("Test!");
            }
        }
    }
}

有人可以帮我给我一个例子吗?预先感谢。

大家好,下面是我的新代码:

using System;
using System.IO;
using System.Linq;

namespace HLB
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            // Our initial data
            string path1 = "";
            if (path1 == "")
                path1 = "F:\\Work\\FLP Code\\test\\log_test.txt";
            var data = File
            .ReadLines(path1)
            .Skip(1) // Skip 1st record (header)
            .Where(line => !string.IsNullOrWhiteSpace(line)) // remove empty lines, if any 
            .Select(line => line.Split('|'))                 // split each line by |
            .Select(items => new {
                Raw = string.Join("|", items),               // Raw data as it is in the file

                Date = items[0].Trim(),
                ID = items[1].Trim(),
                Folder = items[2].Trim(),
                Doc = items[3].Trim(),
                FileName = items[4].Trim(),
                NoOfAct = items[5].Trim(),
            });

                // Now, it's quite easy to represent the data as we want.
                // And we want to *group by* `Folder` and `ID`: just one GroupBy operation
                var result = data
                    .GroupBy(item => new {
                        ID = item.ID,
                        Folder = item.Folder
                    })
                    .Select(chunk =>
                    $"File {chunk.Key.Folder + "_" + chunk.Key.ID}.txt :\r\n  {string.Join(Environment.NewLine + "  ", chunk.Select(item => item.Raw))}");

                foreach (var record in result)
                    Console.WriteLine(record);

            File.WriteAllLines(@"F:\\Work\\FLP Code\\test\\MyNewTextFile.txt", data
            .Where(item => item.ID == "1" &&
            item.Folder == "ABC.20181101.CGLOG")
            .Select(item => item.Raw));
        }
    }
}

2 个答案:

答案 0 :(得分:2)

查询过滤分组总结等)时,请尝试使用 Linq < / em>

using System.Linq;
using System.IO;

...

// Our initial data
var data = File
  .ReadLines(path1)
  .Skip(1) // Skip 1st record (header)
  .Where(line => !string.IsNullOrWhiteSpace(line)) // remove empty lines, if any 
  .Select(line => line.Split('|'))                 // split each line by |
  .Select(items => new {
     Raw  = string.Join("|", items),               // Raw data as it is in the file

     Date = items[0].Trim(),
     ID  = items[1].Trim(),
     Folder = items[2].Trim(),
     Doc = items[3].Trim(),
     FileName = items[4].Trim(), 
     NoOfAct = items[5].Trim(),   
   });

 // Now, it's quite easy to represent the data as we want.
 // And we want to *group by* `Folder` and `ID`: just one GroupBy operation
 var result = data
   .GroupBy(item => new {
      ID = item.ID, 
      Folder = item.Folder})  
   .Select(chunk => 
    $"File {chunk.Key.Folder + "_" + chunk.Key.ID}.txt :\r\n  {string.Join(Environment.NewLine + "  ", chunk.Select(item => item.Raw))}"); 

foreach (var record in result) 
  Console.WriteLine(record);

您会看到类似这样的内容:

File: ABC.20181101.CGLOG_1.txt :
  20181024    |1     |ABC.20181101.CGLOG   |test  |xxxx       |12
File: CDE.20181013.CGLOG_2.txt :
  20181024    |2     |CDE.20181013.CGLOG   |home  |XyyX       |2
... 

编辑:在您的 actual 问题中,让我们创建filesToWrite查询而不是result(我们已用于演示){p >

var filesToWrite = data
  .GroupBy(item => new {
     ID = item.ID,
     Folder = item.Folder
    })
  .Select(chunk => new {
    FileName = Path.Combine(
      @"F:\Work\Outcome", //TODO: put the desired directory here
       chunk.Key.Folder + "_" + chunk.Key.ID + ".txt"),
    Lines = chunk.Select(item => item.Raw)
   });

foreach (var file in filesToWrite) {
  // Let's create the directory if it doesn't exist
  Directory.CreateDirectory(Path.GetDirectoryName(file.FileName));
  File.WriteAllLines(file.FileName, file.Lines);
}

答案 1 :(得分:0)

由于文件夹是您想要的,因此您在检查文件名时是否要检查文件名是否包含所需的字符串。

string[] lines = File.ReadAllLines(path1);
// Start at line number 2 because there is a header
for (int i = 1; i < lines.Length; i++)
{
    // 2 ways to do this:
    if (lines[i].Contains("%ABC%"))
    {
        // Copy it where you want
    }

    // or a more structured way:
    if (lines[i].Split('|')[2].Contains("ABC"))
    {
        // Copy it where you want
    }
}