Operating System - HP-UX
1752780 Members
6157 Online
108789 Solutions
New Discussion юеВ

Re: Date option in scripting

 
SOLVED
Go to solution
Aggy
Frequent Advisor

Date option in scripting

I want to get yesterdays date in below format

Sep 2

This is so that I can write a script to grep the date with below command

yesterday=`TZ=GMT+23 date | cut -d " " -f 2,3,4`
cat /var/adm/syslog/syslog.log | grep "$yesterday" | grep -i -E 'crit|warn|ftp'

The problem is that on Monday I need to get the list from Fridays date ( syslog is just an example and I will be using the script to get other date related logs as well)
How can I achieve this?

I tried caljd.sh and can achieve this but the format is as below, Is there a way that I can change the format

Caljd.sh date:
YESTERDAY=$(caljd.sh -y -s $(caljd.sh -p 3))
echo "Yesterday = ${YESTERDAY}"

яГШ Yesterday = 20070831

The sample script is below, might help you to understand what I want to achieve

#!/usr/bin/sh
yesterday=`TZ=GMT+23 date | cut -d " " -f 2,3,4`
# Caljd command can also be used for friday
#friday=`TZ=GMT+?? date | cut -d " " -f 2,3,4`
HOSTNAME=`uname -n`
weekday=`date -u +%w`

if ((weekday =1))
then
echo "This is $HOSTNAME server"
# syslog
echo "Syslog for $HOSTNAME"
echo
cat /var/adm/syslog/syslog.log | grep "$friday" | grep -i -E 'warn|crit'

else
# syslog
echo "Syslog for $HOSTNAME"
echo
cat /var/adm/syslog/syslog.log | grep "$yesterday" | grep -i -E 'ftp'

fi
6 REPLIES 6
Steven E. Protter
Exalted Contributor

Re: Date option in scripting

Shalom,

Few places to look for date help.

http://hpux.ws/merijn/caljd-2.25.sh

http://hpux.ws/merijn/caljd-2.2.pl

LOGFILE="/tmp/memuse.mon.${FILTER}-`date +%d-%b-%Y`.log"

The above two utilities, written by A. Clay Stephenson do a lot of troublesome date stuff for you.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: Date option in scripting

Hi Aggy:

use perl;

# perl -MPOSIX -e 'print strftime "%b %1d\n",localtime(time-86400)'

'strftime' returns fileds just like you return formatted fields from 'date'. The computations occur in Epoch seconds; hence the current localtime less 86.400 seconds is exactly yesterday.

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: Date option in scripting

Hi (again):

What was it about the Perl script that didn't satisfy your original objective?:

> want to get yesterdays date in below format

Sep 2

# yesterday=$(perl -MPOSIX -e 'print strftime "%b %1d\n",localtime(time-86400)')

ALSO:

You can drop the useless 'cat' and let 'grep' open and read the file:

# grep "${yesterday}" /var/adm/syslog/syslog.log | grep -i -E 'crit|warn|ftp'

Regards!

...JRF...


A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Date option in scripting

You can do it with caljd.sh and get your abbreviated month names AND the same command that can get yesterday can also skip past weekends so that Mon returns last Friday but Tuesday returns Monday. It's actually very simple.

caljd.sh -o $(caljd.sh -x 6 -x 0 -p 1) | read MO DAY YEAR
echo "${MO} ${DAY}"

The -x 6 says skip past Saturdays; -x 0 skips past Sunday, -p 1 says 1 previous day.
-o says print abbreviated month names rather than numeric months.

We read the year but ignore it in the echo. All of this was in the usage of caljd.sh. Invoke as caljd.sh -u for full usage and many examples.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Date option in scripting

Hi (again) Aggy:

My apologies for not reading! You *did* say:

> The problem is that on Monday I need to get the list from Fridays date

Clay's script is an answer that works without re-invention AND has a plethora of useful options. To redeem my failure to read, you could use this Perl script:

# cat .prevworkday
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);
my $t = time();
my $d = strftime( "%u", localtime($t) );
my $n = $d == 1 ? 3 : $d == 7 ? 2 : 1;
print strftime "%b %1d\n", localtime( $t - ( $n * 86400 ) );
1;

...run as:

./prevworkday
Aug 31

Regards!

...JRF...
Aggy
Frequent Advisor

Re: Date option in scripting

Thank You James and A.Clay
With your help I will be able to complete my script