Operating System - HP-UX
1834297 Members
2193 Online
110066 Solutions
New Discussion

How to compare dates in a text file ?

 
SOLVED
Go to solution
Preet Dhillon
Advisor

How to compare dates in a text file ?

Dear Colleagues,

I have a text file containing tape barcode labels and the date they expire. Typically looks like this :

Barcode: 000001 Expire Date: Wed Mar 28 2001
Barcode: 000001 Expire Date: Thu May 16 2002

I'm writing a script which searches this text file and identifies all tapes which have expired. To do this I need a way of taking the sysdate, comparing it with the dates in the file and identifying all dates prior to the sysdate. Is there a way of doing this?
Many thanks and regards,
Preet :-)
Nothing succeeds like excess
5 REPLIES 5
Andreas Voss
Honored Contributor
Solution

Re: How to compare dates in a text file ?

Hi,

here an awk script for you:

awk -vcday=$(date +%d) -vcmon=$(date +%m) -vcyear=$(date +%Y) '{
day=$(NF-1)
if($(NF-2)=="Jan") mon=1
if($(NF-2)=="Feb") mon=2
if($(NF-2)=="Mar") mon=3
if($(NF-2)=="Apr") mon=4
if($(NF-2)=="May") mon=5
if($(NF-2)=="Jun") mon=6
if($(NF-2)=="Jul") mon=7
if($(NF-2)=="Aug") mon=8
if($(NF-2)=="Sep") mon=9
if($(NF-2)=="Oct") mon=10
if($(NF-2)=="Nov") mon=11
if($(NF-2)=="Dec") mon=12
year=$(NF)
expire="No"
if(year < cyear)
expire="Yes"
else if(year == cyear)
{
if(mon < cmon)
expire="Yes"
else if(mon == cmon)
{
if(day < cday)
expire="Yes"
}
}
print $0 ": Expire = " expire
}'

Regards
John Palmer
Honored Contributor

Re: How to compare dates in a text file ?

Hi,

This should be fairly straightforward...

Get today's date in the format yyyymmdd:
TODAY=$(date +%Y%m%d)

Then for each record in your file, convert the date to the same format then compare them:

cat | {
while read X TAPE X X X MM DD YYYY
# convert Month in MM to numeric
do
case ${MM} in
Jan) M=01;;
Feb) M=02;;
Mar) M=03;;
Apr) M=04;;
May) M=05;;
Jun) M=06;;
Jul) M=07;;
Aug) M=08;;
Sep) M=09;;
Oct) M=10;;
Nov) M=11;;
Dec) M=12;
esac

let YYYYMMDD=$(YYYY}${MM}${DD}

if (( YYYYMMDD < TODAY }};
then
fi
done
}

Regards,
John
H.Merijn Brand (procura
Honored Contributor

Re: How to compare dates in a text file ?

Install Date::Calc for perl, available from ftp://download.xs4all.nl/pub/mirror/CPAN/modules/by-module/Date/Date-Calc-5.0.tar.gz
Enjoy, Have FUN! H.Merijn
Preet Dhillon
Advisor

Re: How to compare dates in a text file ?

Hi John,

Many thanks for your help with this.
Just a quick question - is the following line correct :

if (( YYYYMMDD < TODAY }};

It looks like the open brackets (( and braces }} are not matched ?

Regards,
Preet
Nothing succeeds like excess
Preet Dhillon
Advisor

Re: How to compare dates in a text file ?

Problem solved guys, many thanks to all of you for responding so quickly - much appreciated as always.

Best Regards,
Preet :-))
Nothing succeeds like excess