Operating System - Linux
1828161 Members
2602 Online
109975 Solutions
New Discussion

Comparing Dates within files

 
SOLVED
Go to solution
Lloydy
New Member

Comparing Dates within files

Hi,

I have a number of files listed in certain users home directories (/home/username/) on one of my unix boxes and within this file the date is displayed in number format - 26062006.

I would like to create a script that would check the date within these files and if the date is older than X no. of months the script would then display the username from the home directory path.

Im stuck with this as ive not found out a way of comparing dates and finding out if the date is older than 3 months.

Does anyone have any pointers or perhaps have created something similar in the past and could show me what they've done.

Thanks in advance
8 REPLIES 8
Peter Godron
Honored Contributor
A. Clay Stephenson
Acclaimed Contributor

Re: Comparing Dates within files

The difficultly stems from your lack of precision of the term "older than 3 months". For example, what does it mean on 28-May on a leapyear? It's quite easy if you change your 3 months to older than 90 days for example because that has an exact meaning.
If it ain't broke, I can fix that.
Peter Nikitka
Honored Contributor

Re: Comparing Dates within files

Hi,

1) I think it is not important for you to match the 3 month correctly - this timeframe differs in some days depending on the current day of the year.

2) You do not tell us much about how the date is located in the files you want to analyze:
- files are textfiles?
- datespecifier single on a line?
- what pattern (if any?) comes after the datespefifier?
- may there be multiple matches?
- is the first match of '- DATEPATTERN' the one you are looking for or the last or ...?

My assuptions:
- textfiles
- not single on a line
- space after datespec.
- first match


Lets look ...


mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Lloydy
New Member

Re: Comparing Dates within files

A. Clay Stephenson:

90 days would be fine. Because thats near enough 3 months.

Peter Nikitka:

The files are titled .datefile and sit in some users home directories... not all. So yes there could be multiple .datefiles but each users home directory would only have 1 of these files at the most.

There is basically nothing in this file apart from a single date string of text... e.g. 260702006

Nothing else sits in that file whatsoever.

find /home/. -name .datefile|cut -d / -f 4 > /home/adam/found.txt

The above writes all the users with the datefile to a new file. I guess its then a matter of comparing the date in all theses users datefiles to see if the date is older than 90 days. If so write them to another text file and/or display them on screen.

Unfortunantly this is where I become stuck.


Peter Godron
Honored Contributor

Re: Comparing Dates within files

Hi,
as per my earlier post:
#!/usr/bin/sh
while read record
do
a=`echo $record | cut -c0-2`
b=`echo $record | cut -c3-4`
c=`echo $record | cut -c5-8`
FileJD=`./caljd.sh -e $a $b $c`
NowJD=`./caljd.sh`
days=`expr $NowJD - $FileJD`
if [ $days -ge 90 ]
then
echo $record more than 90 days old
fi
done < a.lis

Reads a file (a.lis) line by line and takes the date in format DDMMYYY, works out the Julian date and then establishes the number of days between todays julian date and the record date. If it is greater or equal to 90 days it prints the input record.

For this to work, you will need the caljd.sh script that is available for free at the locations mentioned in the referenced threads.
Peter Nikitka
Honored Contributor

Re: Comparing Dates within files

OK,

try this:

awk -v now=$(date +%Y%m%d) 'function days(str) { num=(substr(str,5,2)-1)*30+substr(str,7)
str-=20000000
num+=substr(str,1,length(str)-4)*365
return num}
BEGIN { today=days(now) }
FILENAME != lastfile && $0 ~ "^[0-9][0-9]*$" {fdays=days($0)
if ((today-fdays) > 90) print FILENAME;lastfile=FILENAME}' file1 file2 ...

Notes:
- I count every month to 30days, a year to 365days
- starting year is 2000
- every file is used once AND if the first file scontains only digits
- you can put this awk in file 'datecmp' and call
find ... | xargs datecmp.awk

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Comparing Dates within files

Okay, now that the problem is better defined I would leverage find, awk, and caljd.sh to handle this task (I'll assume that your homedirs are located in /home):

-----------------------------------------

#!/usr/bin/sh

typeset FNAME=".datefile"
typeset -i MAXDAYS=90

typeset F=""
typeset MO=""
typeset DAY=""
typeset YEAR=""

typeset -i TODAY=$(caljd.sh)
find /home -type f -name "${FNAME}" 2>/dev/null | while read F
do
awk '{if ($0 ~ "[0-9]{8}") {print substr($0,1,2),substr($0,3,2),substr($0,5)}}' "${F}" | read DAY MO YEAR
if [[ -n "${YEAR}" ]] # only need to test YEAR; if not null the 1st 2
# read variables can't be null
then
typeset -i FDAY=$(caljd.sh -e ${DAY} ${MO} ${YEAR})
typeset -i AGE=$((${TODAY} - ${FDAY}))
if [[ ${AGE} -gt ${MAXDAYS} ]]
then
echo "Age: ${AGE} days Homedir: $(dirname ${F})"
fi
fi
done



------------------------------------------

If I didn't make no typing booboo's that should do it and be rather robust as well.


You will also need the caljd.sh script; attached is the latest version; make it executable and place somewhere in your PATH

If it ain't broke, I can fix that.
Peter Nikitka
Honored Contributor

Re: Comparing Dates within files

Hi,

did you get successful hints with the presented solutions?
Was something wrong?

Look at
http://forums1.itrc.hp.com/service/forums/helptips.do?#28

as well.
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"