awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well

awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat

subtract 30 days fails

awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat

awk command in hp unix subtract 30 days automatically from current date without date illegal option error namely

date: illegal option -- -
Usage: date [-u] [+format]
       date [-u] [mmddhhmm[[cc]yy]]
       date [-a [-]sss.fff]

I'm not sure why you are starting another thread to discuss what seems to be the same issue, but apparently in this thread the data you're looking for is in the first field on lines in your data instead of the last field.

As has been said before, providing sample input data (in CODE tags), desired output from that sample input (in CODE tags), and a clear statement of what shell and operating system you're using (including version numbers) would make it much easier to help you.

Does the version of HP-UX you're using have a ksh93 utility? If so, what output do you get from running the command:

printf '%(%s %Y-%m-%d %H:%M:%S)T\n'

in that shell?

If you don't have a ksh93 utility, what do you get if you run that command when using ksh as your shell?

Note that your problem has absolutely nothing to do with awk ; the errors you are getting are from the date utility.

hello
when I run

printf '%(%s %Y-%m-%d %H:%M:%S)T\n'

I get the following error

printf:  Error processing format
printf:  Error processing format
printf:  Error processing format
printf:  Error processing format
printf:  Error processing format
printf:  Error processing format

and the version of ksh I am using 11/16/88

I repeat:

no it doesn't have ksh93 I have Version 11/16/88 the very first 88 release on the HP UX server namely

/bin/ksh:
         $ B.11.31  Oct  1 2008 01:54:10 $
        Version 11/16/88
         blok.c $Date: 2008/08/20 12:15:51 $Revision: r11.31/1 PATCH_11.31 (PHCO_38683)
         expand.c $Date: 2008/10/28 10:48:37 $Revision: r11.31/1 PATCH_11.31 (PHCO_38683)
         builtin.c $Date: 2007/10/25 15:12:29 $Revision: r11.31/1 PATCH_11.31 (PHCO_37285)
         test.c $Date: 2008/05/01 15:55:23 $Revision: r11.31/1 PATCH_11.31 (PHCO_38162)
         strdata.c $Date: 2008/08/25 18:24:40 $Revision: r11.31/1 PATCH_11.31 (PHCO_38683)
         $Revision: @(#) ksh88 R11.31_BL2009_0729_2 PATCH_11.31 PHCO_38683

There usually is a ksh93 version available on HP-UX systems:

/usr/dt/bin/dtksh

But it does not support %T functionality.

I think your best bet would be perl .

perl -MPOSIX -sle 'print strftime("%Y-%m-%d",localtime(time-$n*24*3600))' -- -n=30

--
Note: The --date command line option is GNU date only.

The following won't work on all UNIX systems and since i do not have a HP-UX system at hand you will have to test: 30 days are 30x24=720 hours. Try:

TZ=GMT+720 date

To get the date and time of 30 days ago from now. If your timezone is not GMT adjust the the offset accordingly.

I hope this helps.

bakunin

1 Like

@Bakunin: that looks like a neat trick. It does not seem to work on HP-UX, however (max 24 hours)...
It appears to only work on AIX and IRIX to the full extent.

I found the following maximum time shifts using the date utility with the TZ variable...

AIX:     Works (No limit)
IRIX:    Works (No Limit)
Tru64:   Max. 7 days
Solaris: Max. 7 days (using /usr/xpg4/bin/date)
MacOS:   Max. 7 days
HP-UX:   Max. 1 day
Linux:   Max. 1 day

Of course on Linux one would not need the trick since it has GNU date...

well... you can convert everything to epoch and do math with epoch.

E.g. as a starter...

echo '2012-07-26 15:00:00' | awk -F'[- :]' -v now="$(date +%s)" '{timeE= $2*2629743 + $3*86400 + ($1-1970)*31556926 +$4*3600 +$5*60+$6; printf("%d - %d=%d\n", now, timeE}'

Yes, but +%s is GNU and BSD date only.
So IMO on HP-UX the best bet appears to be perl again:

perl -le 'print time'

If one were to create a sample input file named myname.dat containing:

2018-12-24'0 days ago
2018-12-17'7 days ago
2018-12-10'14 days ago
2018-12-03'21 days ago
2018-11-26'28 days ago
2018-11-25'29 days ago
2018-11-24'30 days ago
2018-11-23'31 days ago
2018-11-22'32 days ago
2018-11-21'33 days ago
2018-11-20'34 days ago
2018-11-19'35 days ago
2018-11-12'42 days ago
2018-11-05'49 days ago

and one were to create a utility named now-30days from the C source file now-30days.c :

#include <stdio.h>
#include <time.h>

#define SecondsPerDay ((time_t)24 * 60 * 60)

int main(void) {
	time_t		Target = time(NULL) - 30 * SecondsPerDay;
	char		TargetString[16];
	struct tm	*Target_tm = localtime(&Target);

	strftime(TargetString, sizeof(TargetString), "%Y-%m-%d", Target_tm);
	printf("%s\n", TargetString);
}

using the command:

make now-30days

and moving the resulting utility into a directory named in your $PATH environment variable, then the awk command:

awk -v t="$(now-30days)" -F "'" '$1 < t' myname.dat

if run in the directory containing myname.dat might produce the output:

2018-11-23'31 days ago
2018-11-22'32 days ago
2018-11-21'33 days ago
2018-11-20'34 days ago
2018-11-19'35 days ago
2018-11-12'42 days ago
2018-11-05'49 days ago

if that command were run on December 24, 2018 (like I did here a few minutes ago).

Is this what you were trying to do?

I don't believe there is anything in this that is not available on all UNIX (including HP-UX), BSD, and Linux systems. It should do some error checking on the returns from localtime() , printf() , and strftime() , but this might give you a starting point for whatever you're trying to do.

1 Like

Oh, I didn't know that.
Then another alternative with awk:

echo '2012-07-26 15:00:00' | awk -F'[- :]'  '{srand();now=srand(); timeE= $2*2629743 + $3*86400 + ($1-1970)*31556926 +$4*3600 +$5*60+$6; printf("%d - %d=%d\n", now, timeE}'

I want to awk data from a log file with dates thus want to filter 30 days before the system date

awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat

but that returns an error

Ooookay - mind to tell us WHAT error?

date: illegal option -- -
Usage: date [-u] [+format]
       date [-u] [mmddhhmm[[cc]yy]]
       date [-a [-]sss.fff]

that is the error returned after I run the code

awk -v t="$(date --date="-30days" +%Y-%m-%d)"-F "" '$1 < t' myname.dat

on Unix ksh93

There is an option in ksh to change the behaviour of ksh to accept latest posix ( or some ksh93 if not all... but no ways for me to test no more HP-UX...) e.g.

 UNIX95= ps -e -o pid,comm,pcpu|more 

So you can always try UNIX95=... and your options, I suggest you have a look at the man pages as I cant having no more access...

If you have perl there's my general purpose date script which is a GNU-date-alike which supports date math.

What output do you get from the following commands when typed into a terminal window running ksh93 :

ksh93 --version
ksh --version
date
printf '%(%Y-%m-%dT%H:%M:%S)T\n' "30 days ago"
1 Like

These time functions also exist in perl:

now_minus_30_days=$(perl -le 'use POSIX; print strftime ("%Y-%m-%d", localtime (time-30*24*60*60))')
awk -v t="$now_minus_30_days" -F "'" '$1 < t' myname.dat

This is a method longhand, but I suspect it is not fully POSIX compliant.
OSX 10.14.1, default bash terminal.

Last login: Mon Jan 21 20:27:49 on ttys000
AMIGA:amiga~> less_days=30
AMIGA:amiga~> day_secs=$(( 24*60*60 ))
AMIGA:amiga~> DATE=$( date -r $(( $( date +%s )-( ${less_days}*${day_secs} ) )) )
AMIGA:amiga~> echo "${DATE}"
Sat 22 Dec 2018 20:33:03 GMT
AMIGA:amiga~> _