LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-15-2010, 05:57 PM   #1
tjay1983
LQ Newbie
 
Registered: Aug 2010
Posts: 12

Rep: Reputation: 0
date subtraction using numbers


Hello

I have logfiles like below.
I want the get the date of the oldest log in this directory and compare it with current date.
Time of the each log can be seen before ".Z" prefix.


I have written the following piece of code. However, it is not working for the following case:

LOGDAY=20101129
TODAY= 20101201

Difference is 72, which is not correct, since these are dates.

How can I fix this?





ls /archive_store

akprd_uwprod_0000015702_1_201011121305.Z
akprd_srprod_0000099047_1_201011121305.Z
...
...
...



#!/bin/sh


DIR=/archive_store
TODAY=`date '+%Y%m%d'`

cd ${DIR}

file=$(ls -1t ${DIR}/ak* | tail -1)
LOGDAY=`echo "$file" | perl -ne 's/.*_(\d{8})[^_]*\.Z$/\1/; print;'`

if [[ `expr $TODAY - $LOGDAY` -lt 4 ]]; then
echo "Please check logs..."
fi
 
Old 11-15-2010, 07:02 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Maybe you should use 'stat -c %Y <file>' to obtain the last modified time of the file in 'seconds since Epoch' rather than going by the name of the file, then you can use subtraction.
 
Old 11-15-2010, 08:34 PM   #3
mac.tieu
Member
 
Registered: Jan 2010
Location: Vietnam
Distribution: Arch
Posts: 65

Rep: Reputation: 22
Try modify you script like:

Code:
#!/bin/sh


DIR=/archive_store
#TODAY=`date '+%Y%m%d'`
TODAY=`date --date="4 days ago" '+%Y%m%d'`
cd ${DIR}

file=$(ls -1t ${DIR}/ak* | tail -1)
LOGDAY=`echo "$file" | perl -ne 's/.*_(\d{8})[^_]*\.Z$/\1/; print;'`

#if [[ `expr $TODAY - $LOGDAY` -lt 4 ]]; then
if [[ `expr $TODAY - $LOGDAY` -lt 0 ]]; then
echo "Please check logs..."
fi
 
Old 11-16-2010, 07:53 PM   #4
tjay1983
LQ Newbie
 
Registered: Aug 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Hi

Thanks for your help.However your sugesstions didint help maybe because the os is hp unix.

mac.tieu:

$ TODAY=`date --date="4 days ago" '+%Y%m%d'`
date: illegal option -- -
Usage: date [-u] [+format]
date [-u] [mmddhhmm[[cc]yy]]
date [-a [-]sss.fff]



kbp:

stat -c %Y akprd_uwprod_0000015702_1_201011121305.Z
sh: stat: not found.
 
Old 11-16-2010, 08:17 PM   #5
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Sorry, didn't realise there was no stat cmd on HPUX. You can get the same info via perl if you like ... http://h21007.www2.hp.com/portal/sit...10275d6e10RCRD

cheers
 
Old 11-17-2010, 01:52 PM   #6
tjay1983
LQ Newbie
 
Registered: Aug 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Hi kbp,

Many thanks for your assistance.
Just for the expriement,I tried to subtract two dates however, I am still getting syntax error.



touch foo

a1=`perl -e 'print (scalar localtime((stat($ARGV[0]))[9]),"\n");' foo`
a2=`date`

echo $a1
Thu Nov 18 08:42:43 2010

echo $a2
Thu Nov 18 08:49:54 NZDT 2010


echo `expr $a2 - $a1`

expr: Syntax error
 
Old 11-17-2010, 02:12 PM   #7
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
This will give you the age of a file (the amount of time since it was most recently modified) in seconds:
Code:
perl -e 'print((time()-(stat($ARGV[0]))[9]),"\n");' foo
This will give you the same datum, but in days, not seconds:
Code:
perl -e 'print((-M $ARGV[0]),"\n");' foo
 
Old 11-17-2010, 06:34 PM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Logically, one could split he dates as yyy mm dd, treating each as a "digit" and subtract lower to higher digits using render/borrow. The problem would be to figure out whether to borrow 31, 30, 29 or 28 as the case may be.

TODAY= 2010 12 01
LOGDAY=2010 11 29

Since 11 is less than 29,borrow a month (30 days in November). This becomes


TODAY= 2010 11 31
LOGDAY=2010 11 29

ie 2 days.

The problem is how to generalise it and test all the border cases.
 
Old 11-18-2010, 02:30 PM   #9
tjay1983
LQ Newbie
 
Registered: Aug 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks wje_lq and Anantha

So, how just to modify my code to suit my needs?
 
Old 11-18-2010, 02:47 PM   #10
tjay1983
LQ Newbie
 
Registered: Aug 2010
Posts: 12

Original Poster
Rep: Reputation: 0
#!/bin/sh

DIR=/archive_store

cd ${DIR}

file=$(ls -1t ${DIR}/ak* | tail -1)
LOGDAY=`echo "$file" | perl -e 'print((-M $ARGV[0]),"\n");'`

if [[ $LOGDAY -lt 4 ]]; then
echo "Please check logs..."
fi



Somehow, below statement doesnt return any value.

LOGDAY=`echo "$file" | perl -e 'print((-M $ARGV[0]),"\n");'`

Last edited by tjay1983; 11-18-2010 at 02:52 PM.
 
Old 11-18-2010, 03:00 PM   #11
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
There are two remaining things to do. The first is that the Perl stuff I suggested took the filename as a "command line" parameter, not on standard input. So you'd have to change this:
Code:
LOGDAY=`echo "$file" | perl -e 'print((-M $ARGV[0]),"\n");'`
to something like this:
Code:
LOGDAY=`perl -e 'print((-M $ARGV[0]),"\n");' "$file"`
And since bash arithmetic works best with integers, you'd want to throw away the fractional part of the value, so do something like this:
Code:
LOGDAY=`perl -e 'print((int(-M $ARGV[0])),"\n");' "$file"`
Play with it and see what you get.
 
1 members found this post helpful.
Old 11-18-2010, 03:57 PM   #12
tjay1983
LQ Newbie
 
Registered: Aug 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Yay, it worked !!

Many thanks for you assitance wje_lq
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple subtraction using bash and bc madpear Programming 2 11-04-2009 01:42 PM
Arrays subtraction in C nesta Programming 13 09-29-2007 02:36 AM
remove week numbers from date navigator in kontact gub Linux - Desktop 0 01-20-2007 01:30 AM
date subtraction(Urgent) Uday123 Fedora 1 04-05-2006 03:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration