I'd like to extract just date and time values out of a log file.
Here are a couple lines from the log file:
2022-05-22 13:51:52,689 STATUS [Thread-5] hoststate.HighwayMonitorObserverImpl.localHighwayStateChanged - Highway State Changed [LOCAL APP_HW] to FAILURE.
2022-05-22 13:51:54,448 STATUS [HostStateManager[0]] hoststate.HostStateManager.a - [0] high way state changes : sys1-a - [OK, OK, OK, null]->[DELAY, OK, OK, null]
2022-05-22 13:51:54,450 STATUS [HostStateManager[0]] hoststate.HostStateManager.a - [0] update necessary
Btw I'm trying to parse all dates from the log files in to another file and I'm stuck at this moment, thanks for everyone who could help me.
CodePudding user response:
For 250kb size file, you can use the below code using Regex.
using System.Text.RegularExpressions;
// read file
string logFile = File.ReadAllText("abc.txt");
// Use regex
Regex regex = new Regex(@"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}");
MatchCollection matches = regex.Matches(logFile);
// print all dates
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
CodePudding user response:
I just did it, using this commandline command:
grep -o "[0-9\-]* [0-9\:]*,[0-9]*" file.txt
Explanation:
[0-9\-]*
covers the date[0-9\:]*
covers the time, minus the milliseconds,
makes the presence of the comma mandatory,[0-9]*
covers the milliseconds, based on the mandatory comma
You can use this as a basis for your regular expression.