Help me in the Execution of Date comparsion


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help me in the Execution of Date comparsion
# 1  
Old 11-25-2013
Help me in the Execution of Date comparsion

Hi ,

Please look into the query

1)Date format: mm/dd/yyyy  example (10/22/2013)
2) compare this date  with the System date 
3) if the difference of dates less than 30 days. Then return true otherwise false.
4)commands date-d is not there in my unix version.
5)present version 6
6)using AIX

Could u provide a sample script of comparing dates and if it less than 30 return true or false.
# 2  
Old 11-25-2013
If you have gawk use can use mktime function for this
# 3  
Old 11-25-2013
You may take a look at this too: datecalc
# 4  
Old 11-25-2013
datecalc is not installed so can u suggest me any alternative
# 5  
Old 11-25-2013
if you have gawk you may try this

Code:
$ echo "10/22/2013 10/30/2013" | \
awk '
    function dates(date){
                          split(date,A,"/")
                          return int(mktime(A[3]" "A[1]" "A[2]" " 00 " " 00 " " 00) / 86400)
                        }
                        {
                              print $0,dates($2)-dates($1), "Days"
                        }'

Resulting
Code:
10/22/2013 10/30/2013 8 Days

To compare Date with System date
Code:
$ echo "10/22/2013" | \
awk '

function Cmd(cmde,result){
                              while ((cmde | getline line) > 0)
                                  {
                                      result = result (result=="" ? "" : "\n")line
                                  }
                              close(cmde)
                              return result
                         }

    function dates(date){
                              split(date,A,"/")
                              return int(mktime(A[3]" "A[1]" "A[2]" " 00 " " 00 " " 00) / 86400)
                        }
                        {
                              diff = dates(Cmd("date +%m/%d/%Y"))-dates($1)
                              msg  = diff < 30 ? "true" : "False"
                              print $0,Cmd("date +%m/%d/%Y"),diff,"Days",msg 
                        }'

Resulting
when date is $ echo "10/22/2013" | \
Code:
10/22/2013 11/25/2013 34 Days False

when date is $ echo "11/22/2013" | \
Code:
11/22/2013 11/25/2013 3 Days true


Last edited by Akshay Hegde; 11-25-2013 at 11:03 AM.. Reason: To fix formatting and codetag
# 6  
Old 11-25-2013
Hi.

Here is a demo of a perl script that accepts a date on the command line. It returns a shell exit status code according to whether the difference in days compared to today's date is less than the default of 30. If a second parameter is given on the command line, that value will be used instead of 30:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate days between dates with perl Date::Calc module.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C perl divepm
pe
pe " Modules used by perl code p1:"
divepm -q -i p1

# Set test date and length:
length=30
TD=${1-"10/22/2013"}

pl " Results for date $TD:"
./p1 $TD && pe " Less than $length" || pe " Greater than $length"

TD="11/15/2013"
pl " Results for date $TD:"
./p1 $TD && pe " Less than $length" || pe " Greater than $length"

# Set a new length.
length=5
pl " Results for date $TD, length $length:"
./p1 $TD $length && pe " Less than $length" || pe " Greater than $length"

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
perl 5.10.0
divepm (local) 1.4

 Modules used by perl code p1:
 1.04	strict
 1.06	warnings
 5.4	Date::Calc

-----
 Results for date 10/22/2013:
 today is 2013, 11, 25, length = 30
 input date for comparison = 2013, 10, 22
 Difference = 34
 Greater than 30

-----
 Results for date 11/15/2013:
 today is 2013, 11, 25, length = 30
 input date for comparison = 2013, 11, 15
 Difference = 10
 Less than 30

-----
 Results for date 11/15/2013, length 5:
 today is 2013, 11, 25, length = 5
 input date for comparison = 2013, 11, 15
 Difference = 10
 Greater than 5

The debugging output can be suppressed as noted in the perl script. Download the file p1.txt, rename it as p1, make it executable, then use it as in the demo script.

Best wishes ... cheers, drl
# 7  
Old 11-29-2013
HI Akshay,

I have tried the code smaple provided by you. I am getting error mktime function not defined, but in my unix box mktime is there. I guess to use mk time i should write any code like usr/bin/ or to use that we havr to write some code i guess.
or If you any alternative is there let me know

---------- Post updated at 04:06 AM ---------- Previous update was at 03:41 AM ----------

Executed this code

$ echo "10/22/2013 10/30/2013" | \
awk '
function dates(date){
split(date,A,"/")
return int(mktime(A[3]" "A[1]" "A[2]" " 00 " " 00 " " 00) / 86400)
}
{
print $0,dates($2)-dates($1), "Days"
}'




Error description:

Function mktime is not defined.
The input line number is 1.
The source line number is 7.
Date.sh[22]: test: argument expected


Any help appreciated
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Answers to Frequently Asked Questions

Compare date in .txt with system date and remove if it's lesser than system date

I m working on shell scripting and I m stuck where in my .txt file there is column as expiry date and I need to compare that date with system date and need to remove all the rows where expiry date is less than system date and create a new .txt with update. (1 Reply)
Discussion started by: Stuti
1 Replies

2. UNIX for Beginners Questions & Answers

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
2 Replies

3. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

4. UNIX for Advanced & Expert Users

File comparsion tool

Hi All, Please suggest some file comparison tool in Linux. The tool have the provision for command line option for file comparison and the output will be stored in to html file. Thanks in advance (2 Replies)
Discussion started by: k_manimuthu
2 Replies

5. Shell Programming and Scripting

String comparsion compare *

Dear all Would anyone tell me how to prevent user from input non asterisk(i.e. *) character via keyboard? #!/bin/ksh targetHour=-1 while ] do echo "Please input target hour": read targetHour done When I execute the above coding, and then input a "j", it return the following... (3 Replies)
Discussion started by: cstsang
3 Replies

6. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

7. Shell Programming and Scripting

File Timestamp and date comparsion

Hi, I have many files in the source directory but I need to process with the latest timestamp file. I am using linux operating system. i want extract the file created timestamp( Ext_File_create_date=) With this format YYYYMMDD- i have searched the relevent command in the unix forms but did... (5 Replies)
Discussion started by: onesuri
5 Replies

8. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

9. Shell Programming and Scripting

Expect Issue Serial Forground Execution vs Concurrent Background Execution

I have an expect script that interrogates several hundred unix servers for both access and directories therein using "ssh user@host ls -l /path". The combination of host/path are unique but the host may be interrogated multiple times if there are multiple paths to test. The expect script is run... (2 Replies)
Discussion started by: twk
2 Replies

10. UNIX for Advanced & Expert Users

map comparsion

Hi all I have to compare maps/files on two seperate boxes and the output must be as following: 1)list the maps/file on box1 2)list the maps/file on box2 3)List maps in both the environments a) which are same b)which are different pls any ideas are appreciated thnks (2 Replies)
Discussion started by: bkan77
2 Replies
Login or Register to Ask a Question