Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

how to subtract time from $date within a script

Highlighted
Occasional Advisor

how to subtract time from $date within a script

Hi I have a script which searches through a log file for certain words. However my log file always has yesterday's date i.e. one day previous

I have log file like the following but of course this will only look for today's date.

myFile="/fnsw/local/logs/elogs/elog"$(date +%Y%m%d);

Can i amend this in some way to look for logs with yesterday's date. I have seen -a option on man date but is that only when setting the date?
6 REPLIES
Acclaimed Contributor Acclaimed Contributor

Re: how to subtract time from $date within a script

Hi Noreen:

Use Perl;

# perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(time-86400)'

...or capture the value like:

# DATE=$(perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(time-86400)')

Of course, 86400 seconds is one-day (24*60*60).

You can create any date format you want using the same formatting directives you would use with 'date(1)'.

Regards!

...JRF...
Regards!

...JRF...
Occasional Advisor

Re: how to subtract time from $date within a script

Hi James,

sorry I should have said it is korn shell script. Can I use localtime with that?

My script is like this

myFile="/fnsw/local/logs/elogs/elog"$(date +%Y%m%d);
serious=$(find $myFile -exec grep -l 'OPERATOR INTERVENTION' {} \;);

if [ -n "$serious" ]; then
mailx -s "Disk Message in FileNet elog" $MLIST < $myFile;
fi
Acclaimed Contributor Acclaimed Contributor

Re: how to subtract time from $date within a script

Hi (again) Noreen:

> sorry I should have said it is korn shell script. Can I use localtime with that?

You can use Perl to create a variable that represents yesterday's date. Your shell script simply invokes this tiny Perl snippet.

...
DATE=$(perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(time-86400)')
myFile="/fnsw/local/logs/elogs/elog${DATE}"
...

The option you are thinking about is no doubt the GNU 'date' command. HP-UX lacks that functionality and we need to leverage Perl. This is no different than writing a shell script with snippets of 'sed' or 'awk'.

Regards!

...JRF...
Regards!

...JRF...

Acclaimed Contributor Acclaimed Contributor

Re: how to subtract time from $date within a script

Hi (again):

Oh, you want a year-month-day format, so re-arrange (as I said you could) the formatting directives. You want:

...
DATE=$(perl -MPOSIX -le 'print strftime "%Y/%m/%d",localtime(time-86400)')
myFile="/fnsw/local/logs/elogs/elog${DATE}"
...

Regards!

...JRF...
Occasional Advisor

Re: how to subtract time from $date within a script

I have no perl installed
Acclaimed Contributor Acclaimed Contributor

Re: how to subtract time from $date within a script

Hi (again) Noreen:

> I have no perl installed

I find that very hard to believe. What is your operating system -- post up 'uname -a'.

Perhaps the perl binary isn't in your PATH. Look for 'perl':

# find /usr /opt -name perl

If this is HP-UX you should find:

/opt/perl_32/bin/perl
/opt/perl_64/bin/perl

Create a symlink from '/usr/bin/perl' to either of the above.

Regards!

...JRF...