Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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.
Posted
Updated 13-Jun-16 14:42pm
Comments
Maciej Los 10-Jun-16 1:52am    
Unclear! Please, be more specific and provide more details. Sample data would be helphul.
Nigol_Learner 10-Jun-16 3:27am    
My all.txt file content alot's of lines like below :
\\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

Each line I want to copy the content "PR-EG4090-28" into 1st column .csv file, and copy the line "\\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
" into 2nd column.

So the output will be like this in .csv file:
"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"


"PR-EG4090-28" < this id already in the line, so I want it to copied/write in 1st column then only copied/write those line in 2nd column.
Nigol_Learner 13-Jun-16 1:29am    
{ var myfile = (@"c:\Log\all.txt");
string[] lin = File.ReadAllLines(myfile);
string pat01 = @"PR-([A-Za-z0-9-]+)";
string pat02 = @"^.*$";
MatchCollection matches = Regex.Matches(lin[0], pat01);
MatchCollection matchess = Regex.Matches(lin[0], pat02);
foreach (Match match in matches)
foreach (Match match1 in matchess)
{
Application x = new Application();
Workbook wb = x.Workbooks.Add();
Worksheet sheet = (Worksheet)wb.Worksheets.get_Item(1);
sheet.Cells[1, 1] = "Prober ID";
sheet.Cells[1, 1].Interior.ColorIndex = 10;
sheet.Cells[1, 3] = "Datalog Path";
sheet.Cells[1, 3].Interior.ColorIndex = 10;

int rowCounter = 2;
int columnCounter = 1;
int columnCounter1 = 3;
{
sheet.Cells[rowCounter, columnCounter] = match.ToString();
columnCounter += 1;
}
{
sheet.Cells[rowCounter, columnCounter1] = match1.ToString();
columnCounter1 += 3;
}

wb.SaveAs(@"D:\DataLocate_PR_id.xlsx");
wb.Close();
x.Quit();
Process.Start(@"D:\DataLocate_PR_id.xlsx");
}
Nigol_Learner 13-Jun-16 1:32am    
Guys, I changing the code those I need what I want, but now the problem is, it only able to get 1st line data, but I want it to get all the lines from "all.txt"? Please help me on this?
Nigol_Learner 13-Jun-16 1:48am    
var myfile = (@"c:\Log\all.txt");
string[] lin = File.ReadAllLines(myfile);
string pat01 = @"PR-([A-Za-z0-9-]+)";
string pat02 = @"^.*$";
foreach (string line in lin)
{
MatchCollection matches = Regex.Matches(line, pat01);
MatchCollection matchess = Regex.Matches(line, pat02);
foreach (Match match in matches)
foreach (Match match1 in matchess)
{


Abit change on the code....

It isn't necessary to load the whole file into memory first. You can process it line-by-line:
C#
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);
          }
        }
      }
    }
  }
}
 
Share this answer
 
Comments
Nigol_Learner 13-Jun-16 21:17pm    
Some of you guys are Godlike.....I'M really appreciate, Thank you very much bro. It's Work for me. Thanks again.
You can try something like this:
C#
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
 
Share this answer
 
v2
Comments
Nigol_Learner 13-Jun-16 5:29am    
string myfile = (@"c:\Log\all.txt");
string writeFile = (@"D:\DataLocate_PR_id.csv");
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);
}
}

Did I done Anything wrong with above code Bro? Because only col1 get right data ready but col2 just empty.
George Jonsson 13-Jun-16 7:48am    
Sorry, my bad. I accidentally wrote Col2 instead of col2 inside the regular expression.
C# is a bit case sensitive.
See the updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900