Is there a way to take text like below (if it was already in an array or a file) and have it strip the lines with a specified date range?
For instance if i wanted every line from 2009-09-04 until 2009-09-09 to be pulled out (maybe this can be done with grep?) how would I go about doing so?
date,test,time,avail
2009-09-01,JS,0.119,99.90
2009-09-02,JS,0.154,99.89
2009-09-03,SWF,0.177,99.90
2009-09-04,SWF,0.177,99.90
2009-09-05,SWF,0.177,99.90
2009-09-06,SWF,0.177,99.90
2009-09-07,SWF,0.177,99.90
2009-09-08,SWF,0.177,99.90
2009-09-09,SWF,0.177,99.90
2009-09-10,SWF,0.177,99.90
Thanks!
(This solution is in PHP — but you can probably do that directly from the command-line, I suppose, with somekind of grep or anything)
Considering your dates are in the
YYYY-MM-DDformat, and that they are at the beginning of each line, you just have to compare the lines alphabetically to compare the dates.One solution would be to :
For the first parts :
And, to iterate over the lines, filtering in/out those you want / don’t want, you could use a foreach loop… Or use the
array_filterfunction, which exists just for this 😉For instance, you could use something like this :
And your callback function would be :
And, the result :
Hope this helps 😉
If your dates where not in the
YYYY-MM-DDformat, or not at the beginning of each line, you’d have toexplodethe lines, and usestrtotime(or do some custom parsing, depending on the format), and, then, compare timestamps.But, in your case… No need for all that 😉