3

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? Also, it would be very useful to consider month as well while filtering

2
  • You meant up to April 25 23:59:59.99999 ?
    – heemayl
    May 1, 2015 at 17:36
  • ohhh yeah i meant 23:59:59.999999 :)
    – riya
    May 1, 2015 at 19:52

1 Answer 1

2

You can very simply add more variables for the day filter:

awk -v start_day=24 -v stop_day=25 -v start_time=10:00:00.000000 -v stop_time=24:00:00.999999 'start_day <= $2 && $2 <= stop_day && start_time <= $3 && $3 <= stop_time' file

yields

files/file1:Apr 24 10:47:00.663137 somedata    
files/file1:Apr 25 23:47:00.663157 somedata    
files/file1:Apr 25 23:47:00.663167 somedata

we're missing a line for Apr 25 because we blindly filter the time to be under 10am. All we have to do is implement a logical test to filter time only for the first day:

awk -v start_day=24 -v stop_day=25 -v start_time=05:00:00.000000 -v stop_time=05:00:00.999999 'start_day <= $2 && $2 <= stop_day && (start_time <= $3 || start_day != $2) && $3 <= stop_time' file

yields

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 

For months you can follow the same idea but you need to translate Apr to 04 (pre-processing or awk magic?) and apply <= => on that.

8
  • Thank you for your reply. What does this actually do? (start_time <= $3 || start_day != $2)
    – riya
    May 1, 2015 at 6:27
  • Filter the time only for the first day
    – Ben
    May 1, 2015 at 6:28
  • It's supposed to work and after testing it seems to work too. The full version of the command being: awk -v start_day=24 -v stop_day=25 -v start_time=10:00:00.000000 -v stop_time=24:00:00.999999 'start_day <= $2 && $2 <= stop_day && (start_time <= $3 || start_day != $2) && $3 <= stop_time' file
    – Ben
    May 1, 2015 at 15:30
  • when I executed this: awk -v start_day=24 -v stop_day=25 -v start_time=05:00:00.000000 -v stop_time=05:00:00.999999 'start_day <= $2 && $2 <= stop_day && (start_time <= $3 || start_day != $2) && $3 <= stop_time' tempFile it displays just "Apr 25 01:47:00.663147 somedata"
    – riya
    May 1, 2015 at 15:42
  • Oh right, if you want to filter the time on the end date then here you go: awk -v start_day=24 -v stop_day=25 -v start_time=05:00:00.000000 -v stop_time=05:00:00.999999 'start_day <= $2 && $2 <= stop_day && (start_time <= $3 || start_day != $2) && (stop_day != $2 || $3 <= stop_time)' file
    – Ben
    May 1, 2015 at 16:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .