Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

how to subtract time from $date within a script

Highlighted
Noreen Merrick_1
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
James R. Ferguson
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...
Noreen Merrick_1
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
James R. Ferguson
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...

James R. Ferguson
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...
Noreen Merrick_1
Occasional Advisor

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

I have no perl installed
James R. Ferguson
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...