✅ Looking for a way to Extract information from a txt File
Looking to Extracting Particular Information form the Log File.
The Information I would like to extract is look for a Particular IP and read the Number of Alarms and then store the X & Y Co-Ords for that number of Alarms.
e.g.
Looking for IP 10.247.247.11
It would return
I will be using the XY Co-ords to draw onto an image.
8 Replies
why not use JSON :SCshocked:
Using a proper structured file format would definitely be better, yes
Failing that, you'll need a simple line parser
Loop over all the lines. If the line starts with
Alarm
, create a new Alarm
. Then, when the line starts with X
, set that alarm's X
, and so onParse each line https://stackoverflow.com/a/8038746 and you could just put everything before your : into the key in a Dictionary<string, string> and everything after into the value. That way you can easily access the values in memory
Stack Overflow
What's the fastest way to read a text file line-by-line?
I want to read a text file line by line. I wanted to know if I'm doing it as efficiently as possible within the .NET C# scope of things.
This is what I'm trying so far:
var filestream = new Syste...
Why not regex? Sometimes can be slow, depends on size of the file but you can kinda get it all at once into reasonably well named groups
[\s]+(?<Time>[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+\.[0-9]+) .*10\.247\.247\.11 .*(?<AlarmNum>[0-9+])[\s]+.*x = (?<X0>[0-9\.]+)[\s]+\.y = (?<Y0>[0-9\.]+)\s+\.sz = (?<Size0>[0-9]+)[\s]+.*x = (?<X1>[0-9\.]+)[\s]+\.y = (?<Y1>[0-9\.]+)\s+\.sz = (?<Size1>[0-9]+)[\s]+.*x = (?<X2>[0-9\.]+)[\s]+\.y = (?<Y2>[0-9\.]+)\s+\.sz = (?<Size2>[0-9]+)[\s]+.*x = (?<X3>[0-9\.]+)[\s]+\.y = (?<Y3>[0-9\.]+)\s+\.sz = (?<Size3>[0-9]+)
How would I use the Variables after?
Like, uh, any other variable?
match.Groups["X0"], "Time", etc as named in the regex
How can I use the Variable of IP into the Regex pattern?
The IP is stored as
IP = "10.247.247.11"