Date and Time comparison using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date and Time comparison using shell script
# 1  
Old 01-08-2012
Date and Time comparison using shell script

Hi,
I'm having two fields in the file
F1|F2
20111220|102000

F1 ->YYYYMMDD
F2 ->HHMMSS

Now, I need to compare this with current date & time and need to return the difference value in hours. Already, I checked with datecalc from the forum. So, need hints from Shell Gurus.

Thanks
# 2  
Old 01-08-2012
Using awk

Code:
cat infile
20111220|102000

Code:
awk -F"|" '{ x=$1;y=$2;
  diff=systime()-mktime(substr(x,0,4)OFS substr(x,5,2)OFS substr(x,7,2)OFS substr(y,0,2)OFS substr(y,3,2)" 00")
  print diff/3600 " Hours"
}' infile

If solaris, use nawk

Using date command
Code:
#!/bin/bash

read data < infile

DATE=${data%%|*}
TIME=$( echo ${data##*|} | sed 's/../&:/g;s/:$//' )

sec=$( date -d "$DATE $TIME" +%s )
curr=$( date +%s )

((diff=(curr-sec)/3600))
echo "$diff Hours"

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 01-08-2012
Thanks for prompt reply.

Anyhow, I'm unable to use systime or mktime; because my OS version is AIX 6.x.

Also, date -d is not a valid option in AIX.
So, please let me know if you have any other alternatives or how can I pass systime/mktime in AIX.

Thanks
# 4  
Old 01-08-2012
See if you have gawk installed.

--ahamed
# 5  
Old 01-08-2012
"gawk" is not installed. We only have awk and nawk in our servers.
# 6  
Old 01-08-2012
Try with nawk in that case

--ahamed
# 7  
Old 01-08-2012
awk and nawk are getting the same error.

awk: Function systime is not defined.

The input line number is 1. The file is abc.log.
The source line number is 2.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date time difference in UNIX shell script

There are 2 dates, Tue Oct 1 13:40:19 2013 Sun Sept 30 10:26:23 2013 I have multiple dates like the above one. How do I calculate the date time difference and display in another column in Shell script. Please help. (3 Replies)
Discussion started by: tanmoysays
3 Replies

2. Shell Programming and Scripting

Date / Time difference in shell script

Request ID GMDCOMTM GMDRRSTIME GMDRESTIME <36812986> : : :I want to display the date -time difference in other fields. Above I have given for only 1 record. I want to calculate for all the records. (GMCOMTM - GMDRRSTM) ,(GMDRRSTM-GMDRESTM) and... (5 Replies)
Discussion started by: ghosh_tanmoy
5 Replies

3. Shell Programming and Scripting

Date / Time difference in shell script

================================================================================ Request ID GMDCOM TIME GMDRRS TIME COM-RRS ================================================================================ <36812974> Tue Oct 1 13:32:40 2013 Tue Oct 1 20:36:42 2013... (1 Reply)
Discussion started by: ghosh_tanmoy
1 Replies

4. Shell Programming and Scripting

How to do simple date (time) calculation in shell script?

Hi, I'm looking for a way to do a simple math calc during a shell script as a means of logging how long a particular task takes. For example... STARTTIME=whenever this script starts ./path/to/command.sh >>logfile.log TOTALTIME=<time at this stage of the script after above command... (7 Replies)
Discussion started by: nbsparks
7 Replies

5. Shell Programming and Scripting

Is it possible to get the date and time of mail which we get into our inbox using shell script?

Hello All, I need a script which extarct the date and time of the mail which is there in our inbox... I can export the mail copy into desktop making it as a textfile or something like that.. So is there anyway to get the date and time from that? (3 Replies)
Discussion started by: smarty86
3 Replies

6. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

7. Shell Programming and Scripting

Date comparison in file to system time

I have a CSV (comma separated vaule) file whose entries resemble Area,\\ntsvsp02\vmcs\download\files\Areas.dat,1,20090303,0,Import Complete,2009-03-02 04:23:00 Product,\\ntsvsp02\vmcs\download\files\items.dat,1,20090303,0,Import Complete,2009-03-02 04:23:00... (3 Replies)
Discussion started by: zainravi
3 Replies

8. Shell Programming and Scripting

shell script to find latest date and time of the files

Hi everyone, Please help:) I have a list of 1000 different files which comes daily to the directory.Some of the files are not coming to the directory now. I need to write a shell script to find the latest date and time of the files they came to the directory. The files should be unique.... (1 Reply)
Discussion started by: karthicss
1 Replies

9. Shell Programming and Scripting

Execute a part of shell script only after particular date and time

I have created a simple shell script... say test.sh Contents of test.sh ================ service named restart cp /etc/imp.conf /backup/test/ #-- if date > 15 July 2007 11:23 pm , then only issue the commans below, else exit --- cp /etc/secondimp.conf /backup/test/ rm -f... (2 Replies)
Discussion started by: fed.linuxgossip
2 Replies

10. Shell Programming and Scripting

Help with time comparison shell script for HP-UX

I am using Korne Shell in HP-Ux. Can someone give me and idea on how I can write a shellscript on how to do this please:- On our HP-UX server, a batch file is run every evening at about 6:30pm. The first step of this batch file will touch an empty "flag" file to indicate that the batch has... (6 Replies)
Discussion started by: gummysweets
6 Replies
Login or Register to Ask a Question