vishal patil

vishal patil

  • NA
  • 1
  • 2.5k

Extracting specific fields from a log file and export it to csv file using C#

Feb 19 2011 1:51 AM
Thanks for your help.
I got the result in new line per record

Output :
35=5 52=20101219-18:05:01.522
35=A 52=20101219-18:06:01.504
35=A 52=20101219-18:06:02
35=1 52=20101219-18:06:02
35=R 52=20101219-18:06:01.847 55=EUR/USD

Now how do I export this output in CSV file?
 
This is the code:
using System;
using System.IO;
using System.Text;

class Program
{
static void Main(string[] args)
{

FileStream fs = new FileStream("sample.log", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string StrFromFile;
StringBuilder ResultStr = new StringBuilder();


while ((StrFromFile = sr.ReadLine()) != null)
{
// your separator char seems to be char 1
string[] SplitStrs = StrFromFile.Split(new char[] {(char)1});
for (int i = 0; i < SplitStrs.Length; i++)
{
if (SplitStrs[i].StartsWith("52="))
{
ResultStr.Append(SplitStrs[i] + " ");
}
else if (SplitStrs[i].StartsWith("55="))
{
ResultStr.Append(SplitStrs[i] + " ");
}
else if (SplitStrs[i].StartsWith("132="))
{
ResultStr.Append(SplitStrs[i] + " ");
}
else if (SplitStrs[i].StartsWith("133="))
{
ResultStr.Append(SplitStrs[i] + " ");
}
else if (SplitStrs[i].StartsWith("35="))
{
ResultStr.Append(SplitStrs[i] + " ");
}
}

Console.WriteLine(ResultStr);
ResultStr.Length = 0;
}
sr.Close();
fs.Close();
Console.ReadKey();
}
}


Answers (1)