I have come across several similar questions, but the solutions seem to be failing me, and I believe it is because I have some junk data in my file that is breaking my attempt to filter by time stamp.
Here is an example file:
2020-12-12 12:10:00,3,4,2
2020-12-12 12:11:00,4,3,2
2020-12-12 12:12:00,4,3,2
2020-12-12 12:13:00,4,3,2
20-12-12 12:14:00,4,3,2
4,3,2
2020-12-12 12:16:00,4,3,1
-12-12 12:17:00,4,3,2
2020-12-12 12:18:00,5,3,2
I'd like to filter this file, creating a new file only containing valid time stamps (each line should start with a valid time stamp).
BegDate="2020-12-12 12:11:00"
EndDate="2020-12-12 12:16:00"
I've tried using sed
TimeWindow=$(sed -rne '/'$BegDate'/,/'$EndDate'/p' $MyFile)
echo $TimeWindow > NewFile.csv
and awk
awk -v from=$BegTime -v to=$EndTime '$1>=from && $1<=to' "$MyFile" > "NewFile.csv"
But both are failing
The desired result for "NewFile.csv" is
2020-12-12 12:11:00,4,3,2
2020-12-12 12:12:00,4,3,2
2020-12-12 12:13:00,4,3,2
2020-12-12 12:16:00,4,3,1
awk -F, -v from="$BegDate" -v to="$EndDate" '$1>=from && $1<=to' "$MyFile"
– Kaffe Myers Mar 03 '22 at 19:34<
,>
,<=
or>=
with out translating it to an epoch timestamp. For doing that you need an OO programming languages. – DanieleGrassini Mar 03 '22 at 19:49<=
/>=
do lexicographical (string) comparisons, not number comparisons. And with thatYYYY-MM-DD HH:MM:SS
timestamp format, lexicographical order is the same as chronological order. – Stéphane Chazelas Mar 03 '22 at 21:10