I have log data on text file (.txt)
The data on text file like this:
City Siren FCT_Tester #1 10039273
Date: 160518, Version: 1.00
ID Test Min Max Unit
1 Battery level 2.85 3.40 V
2 Piezo sound level 1.45 2.80 V
3 Left D3 (Ch 3) light intensity 2000
10000 mcd
4 Right D2 (Ch 1) light intensity 2000
10000 mcd
Date Time Battery level Piezo sound level
Left D3 (Ch 3) light intensity Right D2
(Ch 1) light intensity
Wed, Sep 19 2018 06:47:01 3.372 1.621 9117.000 7303.300
Wed, Sep 19 2018 06:47:19 3.374 1.614 8614.400 7152.300
Wed, Sep 19 2018 08:08:04 3.378 1.604 9586.500 7422.400
Wed, Sep 19 2018 08:08:20 3.375 1.632 9249.300 6682.200
Wed, Sep 19 2018 08:08:44 3.377 1.615 9059.700 6777.100
Wed, Sep 19 2018 08:09:15 3.376 1.626 8902.500 6962.200
What I've done is:
echo count(file($files));
It will get the all rows.
So my question now, I want to count the rows of text file with condition from date and time. Get the count Wed, Sep 19 2018 08:00:00
until Wed, Sep 19 2018 11:00:00
So the result should be: 4
Is it possible?
UPDATE
The log file is using tab, then the code can' read with error:
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string
below is the log file
Tue, Sep 18 2018 23:59:53 3.380 1.622 9958.500 7301.000
Wed, Sep 19 2018 00:00:08 3.380 1.622 8817.900 7194.800
Wed, Sep 19 2018 00:00:28 3.343 1.598 9089.500 7033.800
Wed, Sep 19 2018 00:00:45 3.376 1.602 8789.200 7285.200
Wed, Sep 19 2018 00:01:01 3.376 1.629 8406.000 7295.700
Wed, Sep 19 2018 00:01:17 3.378 1.623 8468.100 7382.800
Wed, Sep 19 2018 00:01:36 3.366 1.619 9462.900 7200.600
Wed, Sep 19 2018 00:01:54 3.370 1.622 9389.700 7018.500
Wed, Sep 19 2018 00:02:21 3.375 1.582 9637.100 7347.500
Wed, Sep 19 2018 00:02:36 3.377 1.595 8775.200 7414.700
Wed, Sep 19 2018 00:02:52 3.340 1.585 8955.300 7376.700
Wed, Sep 19 2018 00:03:20 3.263 1.600 8325.900 6694.700
Wed, Sep 19 2018 00:03:37 3.369 1.616 9554.400 7045.700
I don't know what do you want exactly but for getting a text file line by line his is he code in which you can parse the lines for special text.
You don't have to do any special splitting or regexing. Just use DateTime::createFromFormat and specify your format.
If your file is formated like
Tue,<space>Sep<space>18<tab>2018<tab>23:59:53<tab>[...]
you can use the following format???,?M?d?Y?H:i:s+
which expands to???
- Ignore first 3 characters (wed, mon, etc),
- An comma?
- Some character (space, tab, any)M
- The month as text (Sep, Sept, September, etc)?
- Some character (space, tab, any)d
- The day as number?
- Some character (space, tab, any)Y
- The year as four digits (2008, 2009, etc)?
- Some character (space, tab, any)Y
- The hours as digits in 24h (09, 10, 13, etc):
- An:
i
- The minutes as digits (09, 10, 13, etc):
- An:
s
- The seconds as digits (09, 10, 13, etc)+
- Ignore (but warn!) any trailing charactersSo your code could look like
Edit to reply to your comment providing the real file:
By looking at a
hexdump -C
of your test.txt you can find, that there are two spaces between day and year (last20 20
in the first line).So your format would be
???,?M?d??Y?H:i:s+
. Notice the second?
betweend??Y
.I'm not regex expert but you can try this one: