473,785 Members | 2,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

6 New Member
This is the code that I have written in C# which shows the contains in the sample.log file.

Expand|Select|Wrap|Line Numbers
  1. class FileRead
  2.     {
  3.         public void ReadData()
  4.         {
  5.             FileStream fs = new FileStream("sample.log", FileMode.Open, FileAccess.Read);
  6.             StreamReader sr = new StreamReader(fs);
  7.             sr.BaseStream.Seek(0, SeekOrigin.Begin);
  8.             string str = sr.ReadLine();
  9.             while (str != null)
  10.             {
  11.                 Console.WriteLine("{0}", str);
  12.                 str = sr.ReadLine();
  13.             }
  14.             sr.Close();
  15.             fs.Close();
  16.             Console.ReadLine();
  17.         }
  18.         public static void Main (string[] args)
  19.         {
  20.             FileRead fr = new FileRead();
  21.             fr.ReadData();
  22.         }
  23.     }
  24.  
->The sample.log file contains following data fields
sample.log:
8=FIX.4.39=61 35=534=149=ID E50=FX52=2010 1219-18:05:01.52256 =SAXOQUOTE10=1 71
8=FIX.4.39=81 35=A49=SAXOQUO TE56=IDE34=1 57=FX52=201012 19-18:06:02369=1 98=0108=30141 =Y10=082
8=FIX.4.39=85 35=149=SAXOQUO TE56=IDE34=2 57=FX52=201012 19-18:06:02369=1 112=20101219-18:06:0210=053 

->Now the problem is I just wanted to print the specific fields from sample.log file in column wise manner or export the specific fields in csv file.
->That is the output should be like this :

Output:
9=61 35=5 52=20101219-18:05:01.522
9=81 35=A 52=20101219-18:06:02
9=85 35=1 52=20101219-18:06:02

Please help me with this.
Feb 18 '11 #1
6 1982
Rabbit
12,516 Recognized Expert Moderator MVP
It looks like there's a delimiter. You could just use that to get the fields you want.
Feb 18 '11 #2
Vishal P Patil
6 New Member
thanks for your reply.
Feb 19 '11 #3
Vishal P Patil
6 New Member
This is the code that I have written in C# which shows the contains in the sample.log file which is 110MB in size. I have added the while loop which will check till end of file and print all the records, but I am not getting the first record and also not getting each record in new line

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.  
  10.             FileStream fs = new FileStream("sample.log", FileMode.Open, FileAccess.Read);
  11.             StreamReader sr = new StreamReader(fs);
  12.             string StrFromFile = sr.ReadLine();
  13.  
  14.             StringBuilder ResultStr = new StringBuilder();
  15.  
  16.           while ((StrFromFile = sr.ReadLine()) != null)
  17.           { 
  18.             // your separator char seems to be char 1
  19.             string[] SplitStrs = StrFromFile.Split(new char[] {(char)1});        
  20.             for (int i = 0; i < SplitStrs.Length; i++)
  21.             {   
  22.                 if (SplitStrs[i].StartsWith("52="))
  23.                 {
  24.                     ResultStr.Append(SplitStrs[i] + " ");
  25.                 }
  26.                 else if (SplitStrs[i].StartsWith("55="))
  27.                 {
  28.                     ResultStr.Append(SplitStrs[i] + " ");
  29.                 }
  30.                 else if (SplitStrs[i].StartsWith("132="))
  31.                 {
  32.                     ResultStr.Append(SplitStrs[i] + " ");
  33.                 }
  34.                 else if (SplitStrs[i].StartsWith("133="))
  35.                 {
  36.                     ResultStr.Append(SplitStrs[i] + " ");
  37.                 }
  38.                 else if (SplitStrs[i].StartsWith("35="))
  39.                 {
  40.                     ResultStr.Append(SplitStrs[i] + " ");
  41.                 }   
  42.             }  
  43.          }
  44.             Console.WriteLine(ResultStr);
  45.             sr.Close();
  46.             fs.Close();
  47.             Console.ReadKey();
  48.         }
This is some data from sample.log file :
**sample.log:**

8=FIX.4.39=6335 =049=SAXOQUOTE5 6=IDE34=457=FX5 2=20101219-18:06:32369=310 =003
8=FIX.4.39=6135 =034=449=IDE50= FX52=20101219-18:06:32.13056= SAXOQUOTE10=169
8=FIX.4.39=6335 =049=SAXOQUOTE5 6=IDE34=557=FX5 2=20101219-18:07:02369=410 =003
8=FIX.4.39=6135 =034=549=IDE50= FX52=20101219-18:07:02.50156= SAXOQUOTE10=170

Also I am not getting why it is not showing the first record and how do I write this result to CSV file?

I am getting output like this,
35=0 52=20101219-18:06:32.130 35=0 52=20101219-18:07:02 35=0 52=20101219-18:07:02.501

but I want the output like this,
35=0 52=20101219-18:06:32.130
35=0 52=20101219-18:07:02
35=0 52=20101219-18:07:02.501

Please help me with this.
Feb 22 '11 #4
Rabbit
12,516 Recognized Expert Moderator MVP
After each line, you need to append a new line. Right now you're only appending spaces.
Feb 22 '11 #5
Vishal P Patil
6 New Member
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 :
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.  
  10.             FileStream fs = new FileStream("sample.log", FileMode.Open, FileAccess.Read);
  11.             StreamReader sr = new StreamReader(fs);
  12.             string StrFromFile;
  13.             StringBuilder ResultStr = new StringBuilder();
  14.  
  15.  
  16.           while ((StrFromFile = sr.ReadLine()) != null)
  17.           { 
  18.             // your separator char seems to be char 1
  19.             string[] SplitStrs = StrFromFile.Split(new char[] {(char)1});        
  20.             for (int i = 0; i < SplitStrs.Length; i++)
  21.             {   
  22.                 if (SplitStrs[i].StartsWith("52="))
  23.                 {
  24.                     ResultStr.Append(SplitStrs[i] + " ");
  25.                 }
  26.                 else if (SplitStrs[i].StartsWith("55="))
  27.                 {
  28.                     ResultStr.Append(SplitStrs[i] + " ");
  29.                 }
  30.                 else if (SplitStrs[i].StartsWith("132="))
  31.                 {
  32.                     ResultStr.Append(SplitStrs[i] + " ");
  33.                 }
  34.                 else if (SplitStrs[i].StartsWith("133="))
  35.                 {
  36.                     ResultStr.Append(SplitStrs[i] + " ");
  37.                 }
  38.                 else if (SplitStrs[i].StartsWith("35="))
  39.                 {
  40.                     ResultStr.Append(SplitStrs[i] + " ");
  41.                 }   
  42.             }
  43.  
  44.             Console.WriteLine(ResultStr);
  45.             ResultStr.Length = 0;
  46.          }
  47.             sr.Close();
  48.             fs.Close();
  49.             Console.ReadKey();
  50.         }
  51.     }
Feb 22 '11 #6
Rabbit
12,516 Recognized Expert Moderator MVP
You'll need a stream writer object to write a file.
Feb 22 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1632
by: Lizim | last post by:
I am relatively new to the C# language and need to extract data from a text file. I would like to be able to search the file which is tab delimmited and extract various fields. I have been using StreamReader to read the file but I am not sure of the method that actually searches for relevant fields and then extracts them. -- Liz :-)
0
1205
by: David Talbot | last post by:
I have a web application that featues the ability to export data to excel. The application opens in a popup window from the login page that detects if the application's popup window is blocked and instructs the user to "Always allow popups from this site". After the user has logged into the application, they choose the excel export. The application opens a popup window to the file export page. You hear the explorer "blip" that tells me...
0
1827
by: William \(Bill\) Vaughn | last post by:
If you start looking for the File | Export Template wizard and can't find it, try reinstalling the Visual Basic settings (Tools | Import and Export Settings). Apparently when you first install VS 2005 the initial setup routines don't wire up this feature correctly. I hear this won't be fixed. -- ____________________________________ William (Bill) Vaughn Author, Mentor, Consultant
3
6015
by: hbarnett | last post by:
I am in process of converting Access 97 databases to Access 2003. Some of the Access 97 databases export data to a text file with a non standard file extension, such as "ext" or "NAL". When I try to run the export in the converted Access 2003 database, I get the error "Cannot update: Database or object is read-only." It seems that the real problem is that I can only export a text file if I use either "txt", "csv", "tab", or "asc" as...
6
4339
by: Vic4338 | last post by:
I have a database with 50000 records containing phone numbers, I need to export the phone numbers sorted by area code, and create/save to .txt using the area code as the phone number. I create the export template using the wizard but cant get the code to run off my form button. Thanks any help is greatly appreciated.
0
1481
by: sgsiaokia | last post by:
I need help in extracting data from another source file using VBA. I have problems copying the extracted data and format into the required data format. And also, how do i delete the row that is not required in the output file, in the below example: The row, D0, is not needed. An Example Data Format From the SOURCE file: W1 W2 W3 W4 Oct05 AverageYield 95% 96% 92% 91% 94% D0 0.1 ...
4
2556
by: HoganGroup | last post by:
I need to create an export file for the following brutal query to upload into a state database (go figure it's a government report!) All was good in my world until I tried to export. Manually or TransferText, didn't matter, it just hated me. The above error stopped me cold in my tracks. Searching about, no one else seems to know what the heck this stupid error is. Anybody? Bueller? SELECT DISTINCT Format(!!,"mm/yyyy") AS ,...
0
869
by: dorothywtkhk | last post by:
Dear All, I got a problem with the following case: I would like to enable a button after the file export (using asp.net), the code is: Dim fi As System.IO.FileInfo = New System.IO.FileInfo(strPath) Response.Clear() Response.AddHeader("Content-Disposition", "attachment;filename=" + fi.Name) Response.AddHeader("Content-Length", fi.Length.ToString()) Response.ContentType = "Text Documents (*.txt)" Response.WriteFile(fi.FullName)
0
1191
by: eyemustbecrazy | last post by:
Hi, I know how to export whole tables as follows... <elsnipo> 'Export This Table strTable = "tblthis" strSql = "SELECT " & strTable & ".* INTO " & strTable & " IN """ &
6
4984
by: Ramesh | last post by:
Hello, I am using the ofstream class to create a text file with keys and values like: Key1=Value10 Key2=Value15 Key3=Value20 In case I need to set a new value for Key2, say value50 - I am able to
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10153
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.