- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Comparing Dates within files
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2006 03:33 AM
07-13-2006 03:33 AM
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
Solved! Go to Solution.
- Tags:
- date arithmetic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2006 03:47 AM
07-13-2006 03:47 AM
Re: Comparing Dates within files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2006 03:47 AM
07-13-2006 03:47 AM
Re: Comparing Dates within files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2006 03:56 AM
07-13-2006 03:56 AM
Re: Comparing Dates within files
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2006 09:21 PM
07-13-2006 09:21 PM
Re: Comparing Dates within files
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2006 10:18 PM
07-13-2006 10:18 PM
Re: Comparing Dates within files
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2006 11:21 PM
07-13-2006 11:21 PM
Re: Comparing Dates within files
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
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2006 02:36 AM
07-14-2006 02:36 AM
Solution-----------------------------------------
#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2006 12:06 AM
07-17-2006 12:06 AM
Re: Comparing Dates within files
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.