如何从文本文件的每一行获取特定单词并插入第一列csv文件,所有文本文件内容在第二列? [英] How to get specific word from text file each line and insert into 1st column csv file with all text file contents at second column?

查看:78
本文介绍了如何从文本文件的每一行获取特定单词并插入第一列csv文件,所有文本文件内容在第二列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var  myfile =( @  C:\Log\all.txt); 
string [] lin = File.ReadAllLines(myfile);
string pat01 = @ PR - ( [A-ZA-Z0-9 - ] +);
MatchCollection matches = Regex.Matches((lin [ 1 ]),pat01);
foreach (匹配匹配 匹配)
{
string writeFile = @ E:\ DataLocate_PR_id.csv< /跨度>;
string readFile = @ C:\\ \\Log\all.txt;
StringBuilder csv = new StringBuilder();
csv.AppendLine(match + readFile);
File.AppendAllText(writeFile,csv.ToString());
}





我的尝试:



这是我的尝试,很多代码很难理解,我从其他论坛上读到。

解决方案

没有必要加载整个文件先存入内存。您可以逐行处理:

 使用 System.IO; 
使用 System.Text.RegularExpressions;

命名空间 ConsoleApplication17
{
class Program
{
私有 静态正则表达式= new 正则表达式( @ (PR \ * * -\ * * [A-Za] z0-9 - ] +),RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
静态 void Main( string [] args)
{
string myFile = @ .. \..\TextFile1.txt;
string writeFile = @ .. \\ \\..\TextFile1.csv;
使用(TextWriter writer = new StreamWriter(writeFile))
{
foreach 字符串 文件.ReadLines(myFile))
{
匹配m = Pattern.Match(line);
if (m.Success)
{
writer.Write(m.Groups [ 1 ]值)。
writer.Write( );
writer.WriteLine(line);
}
}
}
}
}
}


你可以尝试这样的事情:

  string  input = File.ReadAllText(myfile); 
正则表达式正则表达式= 正则表达式( @ ^(小于COL2> *(小于COL1>??PR\s * -\s * [A-ZA-Z0-9 - ] +)*)

,RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline);
MatchCollection matchCollection = regex.Matches(input);
使用(TextWriter tw = new StreamWriter(writeFile))
{
foreach (在 matchCollection中匹配m
{
string col1 = m.Groups [ col1]。值; // 仅提取的部分
string col2 = m.Groups [ col2]。 // 完整行
tw.WriteLine( {0},{1},col1,col2);
}
}



结果

 PR-EG4090-28,\ \192.168.8.212\c\Avago.ATF.Common\Clotho_SDI_Data\FBAR_2MS5-1505_N3131-3033_PR-EG4090-28_FULL-1PASS_CHAN1_20160610_104333_IP192.168.0.2_G6AABRH 


var myfile = (@"C:\Log\all.txt");
                string[] lin = File.ReadAllLines(myfile);
                string pat01 = @"PR -([A-Za-z0-9-]+)";
                MatchCollection matches = Regex.Matches((lin[1]), pat01);
                foreach (Match match in matches)
                {
                    string writeFile = @"E:\DataLocate_PR_id.csv";
                    string readFile = @"C:\Log\all.txt";                                   
                    StringBuilder csv = new StringBuilder();
                    csv.AppendLine(match + readFile);
                    File.AppendAllText(writeFile, csv.ToString());                
                }



What I have tried:

This is my 1s try and many of code hard to understand that I read from other forums.

解决方案

It isn't necessary to load the whole file into memory first. You can process it line-by-line:

using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication17
{
  class Program
  {
    private static Regex Pattern = new Regex(@"(PR\s*-\s*[A-Za-z0-9-]+)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
    static void Main(string[] args)
    {
      string myFile = @"..\..\TextFile1.txt";
      string writeFile = @"..\..\TextFile1.csv";
      using (TextWriter writer = new StreamWriter(writeFile))
      {
        foreach (string line in File.ReadLines(myFile))
        {
          Match m = Pattern.Match(line);
          if (m.Success)
          {
            writer.Write(m.Groups[1].Value);
            writer.Write(",");
            writer.WriteLine(line);
          }
        }
      }
    }
  }
}


You can try something like this:

string input = File.ReadAllText(myfile);
Regex regex = new Regex(@"^(?<col2>.*(?<col1>PR\s*-\s*[A-Za-z0-9-]+).*)


", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline); MatchCollection matchCollection = regex.Matches(input); using (TextWriter tw = new StreamWriter(writeFile)) { foreach (Match m in matchCollection) { string col1 = m.Groups["col1"].Value; // Only the extracted part string col2 = m.Groups["col2"].Value; // The full line tw.WriteLine("{0},{1}", col1, col2); } }


Result

PR-EG4090-28,\\192.168.8.212\c\Avago.ATF.Common\Clotho_SDI_Data\FBAR_2MS5-1505_N3131-3033_PR-EG4090-28_FULL-1PASS_CHAN1_20160610_104333_IP192.168.0.2_G6AABRH


这篇关于如何从文本文件的每一行获取特定单词并插入第一列csv文件,所有文本文件内容在第二列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆