Reputation: 35
I'd like to fetch the data between "Apr 24 10:00:00.000000" and "Apr 25 24:00:00.999999" in the following log but I am not sure how to get this work:
files/file1:Apr 22 02:47:00.663117 somedata
files/file1:Apr 23 04:47:00.663127 somedata
files/file1:Apr 24 05:47:00.663137 somedata
files/file1:Apr 24 10:47:00.663137 somedata
files/file1:Apr 25 01:47:00.663147 somedata
files/file1:Apr 25 23:47:00.663157 somedata
files/file1:Apr 25 23:47:00.663167 somedata
files/file1:Apr 26 23:47:00.663177 somedata
I tried using the following command but that would only filter by time and not consider date as well:
awk -v start=10:00:00.000000 -v stop=24:00:00.999999 'start <= $3 && $3 <= stop'
I would like only the following data to be fetched:
files/file1:Apr 24 10:47:00.663137 somedata
files/file1:Apr 25 01:47:00.663147 somedata
files/file1:Apr 25 23:47:00.663157 somedata
files/file1:Apr 25 23:47:00.663167 somedata
Can someone please help with this?
Upvotes: 2
Views: 421
Reputation: 8140
$ cat get_dates.awk
BEGIN {
start_t = mktime("2015 04 24 10 0 0")
end_t = mktime("2015 04 26 0 0 0")
FS="[:. ]"
for (i = 1; i <= 12; i++) {
timestamp = mktime("2015 " i " 1 0 0 0")
m = strftime("%b", timestamp)
month[m] = i
}
}
{
now = mktime("2015 " month[$2] " " $3 " " $4 " " $5 " " $6)
if (start_t <= now && now <= end_t) {print}
}
In the first block, we create time stamps for the start and the end of the time to look at, and we set the field separator to colon, space, or point. Then we create an array that converts the abbreviated Month to a number.
Then, for each line, we create a new timestamp with the corresponding fields, and if it falls between the two timestamps above, we use it.
$ awk -f get_dates.awk dates.txt
files/file1:Apr 24 10:47:00.663137 somedata
files/file1:Apr 25 01:47:00.663147 somedata
files/file1:Apr 25 23:47:00.663157 somedata
files/file1:Apr 25 23:47:00.663167 somedata
Upvotes: 1