Friday, 23 August 2013

Bash find last entry before timestamp

Bash find last entry before timestamp

I have a .csv file that is formatted thus;
myfile.csv
**Date,Timestamp,Data1,Data2,Data3,Data4,Data5,Data6**
20130730,22:08:51.244,APPLES,Spain,67p,blah,blah
20130730,22:08:51.244,PEARS,Spain,32p,blah,blah
20130730,22:08:51.708,APPLES,France102p,blah,blah
20130730,22:09:62.108,APPLES,Spain,67p,blah,blah
20130730,22:08:51.244,APPLES,Spain,67p,blah,blah
I wish to feed in a timestamp which most likely will NOT match up
perfectly to the millisecond with those in the file, and find the
preceding line that matches a particular grep search.
so e.g. something like;
cat myfile.csv | grep 'SPAIN' | grep 'APPLES' | grep -B1 "22:09"
should return
20130730,22:08:51.244,APPLES,Spain,67p,blah,blah
But thus far I can only get it to work with exact timestamps in the grep.
Is there a way to get it to treat these as a time series? (I am guessing
that's what the issue is here - it's trying pure pattern matching and not
unreasonably failing to find one)

No comments:

Post a Comment