awk comparison of dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk comparison of dates
# 1  
Old 03-21-2016
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.

Code:

 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
# 2  
Old 03-21-2016
What UNIX/Linux do you have? The GNU version of awk does time/date operations.
# 3  
Old 03-21-2016
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.
# 4  
Old 03-21-2016
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:
Code:
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.
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 03-22-2016
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk if comparison with variable

Hi , Need help, p for value in `awk -F, '{print $1 }' ad | uniq` do x=$(echo $value) echo $x echo `awk -F, '{if( $1 == $x) sum = sum + $8 } END{print sum}' ad` --- not working echo `awk -F, '{if($1 == “MT”) sum = sum + $8 } END{print sum}' ad` -- Working but hard coded done; ad... (4 Replies)
Discussion started by: nadeemrafikhan
4 Replies

2. Shell Programming and Scripting

awk comparison with two patterns

Here is my list, which contains URLs for file downloads: //servername.com/version/panasonic1,1_1.1.1 //servername.com/version/panasonic3,1_6.7.1 //servername.com/version/panasonic3,2_6.8 //servername.com/version/panasonic2,6_3.0.2 //servername.com/version/panasonic3,1_7.1.3... (5 Replies)
Discussion started by: ibanezpete
5 Replies

3. Shell Programming and Scripting

awk comparison not working

Can you please help me on belw awk comparsion which doest not work cat employee_list NAME Last-login Jack 03/25/2013 Maneul 03/26/2013 Eric 03/26/2013 Samuel 03/28/2013 loak 03/29/2013 zac 03/29/2013 this is my awk .. it gives me error cat employee_list | awk '(($2=='date... (3 Replies)
Discussion started by: Sara_84
3 Replies

4. Shell Programming and Scripting

Get time different between two dates using awk.

Hello, I have looked at perados date topics and couldn't find one that would suit my needs. I have the output below from Microsoft sql server. It shows where there was a status change. The date time in column two and the status indicator is in column 4. In the first two rows it goes from... (7 Replies)
Discussion started by: benefactr
7 Replies

5. Shell Programming and Scripting

AWK string comparison

Hey guys.. New in linux scripting and need some help on some scripting with history command. I managed to export the command history into a file and now i'm trying to select from that file some specific commands that were made in a certain period. Here's what i got so far echo -n... (2 Replies)
Discussion started by: mishu_cgm
2 Replies

6. Shell Programming and Scripting

File comparison using awk

my files are as follows fileA sepearated by tab /t 00 lieferungen 00 attractiop 01 done 02 forness 03 rasp 04 alwaysisng 04 funny 05 done1 fileB funnymou120112 funnymou234470 mou3raspnhdhv rddfgmoudone1438748 so all those record which are greater than 3 and which are not... (6 Replies)
Discussion started by: rajniman
6 Replies

7. Shell Programming and Scripting

awk comparison and substitution

Hi, here's my - not so easy to describe - problem: I want to compare the values of one file (FileA) with a cutoff-value and, if this comparison is true, substitute those values with those in the second file (FileB). However, there are many FileA's (FileA), whereas there is only one FileB. Every... (10 Replies)
Discussion started by: waddle
10 Replies

8. Shell Programming and Scripting

awk comparison

Hello all, Probably a very simple question, I am stuck with a small part of a code: I am trying to do a comparison to get the maximum value of column 6 if columns 1, 4 and 5 of two or more rows match. Here is what I am doing: awk -F'\t' '{if ($6 > a)a=$6}END{for (i in a) print i"\t"a}' ... (4 Replies)
Discussion started by: jaysean
4 Replies

9. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

10. UNIX for Dummies Questions & Answers

multiple comparison in awk

I have an input file. Each line in it has several characters. If the first three characters of the line is '000' or '001' or '002' or '003', I need to print it in output. How can I do this in awk. I am able to do if the search string is only one (let us say 000). cat <filename> | awk... (1 Reply)
Discussion started by: paruthiveeran
1 Replies
Login or Register to Ask a Question