The program will be run on a similar file like the one below:
03/09/11 09:46:28.771 This is sample / test text
03/09/11 09:46:28.771 This is sample / test text
03/09/11 09:46:28.771 This is sample / test text
03/09/11 09:46:28.771 This is sample / test text
<test message needs to be deleted>
03/09/11 09:46:28.771 This is sample / test text
03/09/11 09:46:28.771 This is sample / test text
03/09/11 09:46:28.771 This is sample / test text
03/09/11 09:46:28.771 This is sample / test text
<test message needs to be deleted>
The output needs to be the following:
- The date & time part only of each line
- duplicate lines need to be removed
- Blank lines need to be removed
- Any other lines conataining text which does not begin with the date time part needs to be removed
I have started coding this as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.IO;
namespace Remove
{
class clsProgram
{
static void Main(string[] args)
{
try
{
// Initialise array which will read all lines in the log file
string[] sLogs = File.ReadAllLines(@"C:test");
StreamWriter sw = File.CreateText(@"C:test2");
// DateTime variable sets the "previous time" as null
DateTime? previousDateTime = null;
// For every line in array sDateLogs which contains the search string
foreach (string sLog in sLogs.Where(log => log.Contains("/")))
{
// Initiate DateTime variable "Current DateTime"
DateTime currentDateTime;
// String TimeStamp removes everything after the first 21 characters so we are left
with the date time stamp only
string sTimeStamp = sLog.Remove(17);
Console.WriteLine(sDateTime);
sw.WriteLine(sDateTime);
}
// Close StreamWriter
sw.Close();
}
// Error handling
catch (Exception ex)
{
Console.WriteLine(String.Format("Error: {0}", ex.Message));
}
}
}
}
So far the program removes the first 17 characters to give the date and time part. The next steps required are to:
- search through line by line, comparing the previous line to the current and if they match then remove the duplicate. I have started this by initiating the previousDate variable but am stuck with taking it further.
- remove any line which does not begin with the date part
- remove any blank lines.
I understand and can describe how to go about doing what I want but having difficulty coding it.
Edited by csharpit, 06 September 2011 - 01:37 PM.
Corrected title


Sign In
Create Account

Back to top









