awk comparison of dates

I need to use awk to return lines in multiple files that contain a date between a start date and end date. The format of the date is as seen in column 3 in the following line.

 A,1458147240,Mar 30 2015 12:54:00PM,s15u4chn ,2,GPS Major Alarm `clear`,component.Channel,10,15,0,138,183,,,Mar 16 2016 12:54:00PM,1100000000000000000000000000000044437
 

What pattern would work to compare or convert this date format to a start date of say 3/29/2015 13:00 and an end date of 4/3/2015 13:00? I can format the start and end date in any format. I'm a rookie and don't have a clue how to compare dates. Also, I am using Putty Link to send this command to a remote machine through a script file if that makes a difference. The reason for this is that I previously used column 2 which is a numeric time stamp based on UTC but I get inaccurate results during DST. Other simple solutions are also welcome.

Thanks

What UNIX/Linux do you have? The GNU version of awk does time/date operations.

The version is Red Hat Enterprise Linux Server release 5.11. It would also be great for the same solution to work with Solaris 10. I need to support the older systems as wall as the new ones.

You are out of luck on Solaris unless gawk (GNU awk) is installed. So can you format the date as "%s" meaning epoch seconds?

If you can do that for

  1. each line of the file
  2. the start date you want
  3. end date you want

Example:

start=1458507311
end=1458607311
#(Solaris uses nawk, not awk)
awk -v end=$end -v start=$start ' $3>=start && $3 <= end'  oldfile > newfile

epoch seconds are the number of seconds since Midnight Jan 1, 1970.

1 Like

Thank you for the quick and clear response. I will give that a try tomorrow. It should do the trick

---------- Post updated at 09:29 AM ---------- Previous update was at 09:28 AM ----------

Jim, I can get this to work as you described but I've run into another issue with time zones. The date in the above example, Mar 30 2015 12:54:00PM, is the local time at the remote machine. Ideally, I would like to be able to have the desired start date and end date be relative to the remote machine rather than my local machine and not have to worry about knowing/including the time zone or adjusting for DST. Is there a way to convert the date text to something viable for comparison using the %s in the awk command?