I'm trying to parse a log file containing info like this:
2015-03-08 10:30:01 /user849/connect
2015-03-08 10:30:01 /user262/open-level2-price
2015-03-08 10:30:01 /user839/open-detailed-quotes
2015-03-08 10:30:02 /user145/add-technical-drawing
2015-03-08 10:30:02 /user108/connect
2015-03-08 10:30:03 /user850/filter-changed
2015-03-08 10:30:03 /user818/open-level2-price
2015-03-08 10:30:03 /user841/column-width
2015-03-08 10:30:03 /user850/filter-changed
2015-03-08 10:30:04 /user850/connect
2015-03-08 10:30:04 /user420/duration
2015-03-08 10:30:04 /user851/filter-changed
2015-03-08 10:30:04 /user217/duration
2015-03-08 10:30:05 /user82/update-column-properties
2015-03-08 10:30:05 /user809/open-level2-price
2015-03-08 10:30:05 /user382/add-technical-drawing
2015-03-08 10:30:06 /user851/connect
2015-03-08 10:30:07 /user350/add-technical-drawing
2015-03-08 10:30:09 /user849/filter-changed
2015-03-08 10:30:09 /user842/sort
2015-03-08 10:30:09 /user849/open-market-watch
2015-03-08 10:30:10 /user429/interval
2015-03-08 10:30:10 /user218/change-columns
2015-03-08 10:30:11 /user749/connect
2015-03-08 10:30:13 /user759/open-detailed-quotes
2015-03-08 10:30:14 /user753/connect
2015-03-08 10:30:14 /user377/connect
I'm trying to find the 3 most used operations, and their percentages, what I have in mind is reading the file, parsing the lines with some regular expressions, or filling them to a data table and then processing that data table, but i was not able to do it.
Can you advice me what to do, where to start, or maybe some code sample (Preferably c#) ?
Thanks in advance!
Edit:
Well, (I've successfully done it now) As for what I've tried, here's is my code
string filePath = @"6458.log";
try
{
DataTable logLines = new DataTable("LogLines");
//logLines.Columns.Add(new DataColumn("DateTime", System.Type.GetType("System.DateTime")));
logLines.Columns.Add(new DataColumn("User", typeof(string)));
logLines.Columns.Add(new DataColumn("Operation", typeof(string)));
string[] lines = System.IO.File.ReadAllLines(filePath);
foreach (string line in lines)
{
var cols = line.Split(new char[] { ' ', '/' }, StringSplitOptions.RemoveEmptyEntries);
DataRow dr = logLines.NewRow();
//dr["DateTime"] = cols[0] + " " + cols[1];
dr["User"] = cols[2];
dr["Operation"] = cols[3];
logLines.Rows.Add(dr);
}
var query = from row in logLines.AsEnumerable()
group row by row.Field<string>("Operation") into operations
orderby operations.Count() descending
select new
{
Name = operations.Key,
CountOfClients = operations.Count()
};
}
catch (Exception ex)
{
throw(ex) ;
}
Please back your hint with code for further explanation!
Thanks Again