0

I want to modify this command to subtract 30 days from current date automatically

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

When I try

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

I get the following error; date: illegal option

I want to do this without having to convert the dates to epoch time in the file.

  • 1
    Please tag your question with the relevant tool/s. Users who know the Unix shell commands are likely to miss a question that has neither got the unix nor the shell tag. So to attract the right people to your question and increase your chances of a qualified answer… – Ole V.V. Dec 20 '18 at 08:53

1 Answers1

0

@edit: The following will work with GNU date only:

You can always subtract seconds.

date --date=@$(($(date +%s) - 30 * 24 * 3600)) +%Y-%m-%d

If you are interested in subtracting 30 days form "now", just:

date --date="-30days" +%Y-%m-%d

date date formatting is so broad, it's good to specify the exact date with for example -I option, from man date:

   -I[FMT], --iso-8601[=FMT]  
          output  date/time  in ISO 8601 format.  FMT='date' for date only
          (the default), 'hours', 'minutes', 'seconds', or 'ns'  for  date
          and    time    to    the    indicated    precision.     Example:
          2006-08-14T02:34:56-06:00

The following:

date --date="$(date -I) -30days" +%Y-%m-%d

works on my system as expected.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • could you show me how you incorporated it with the awk command coz when I do so still get the date illegal option error on unix HPUX server – Kasiiku Mark Dec 20 '18 at 13:56
  • `awk -v="$(date --date="-30days" +%Y-%m-%d)" '{print}'` – KamilCuk Dec 20 '18 at 14:59
  • Run the code as awk -v t=$(date--date="-30days" +%Y-%m-%d) -F "'" '$1 < t' myname.dat still get the error sh: date--date=-30days: not found. – Kasiiku Mark Dec 21 '18 at 06:25
  • `-v t="$(date --date="-30days" +%Y-%m-%d)"` There is a space between `date` and `--date`. See [turorialspoint](http://tpcg.io/MV0y7y). – KamilCuk Dec 21 '18 at 17:37
  • run the command awk -v t="$(date --date="-30days" +%Y-%m-%d)"-F "" '$1 < t' myname.dat still the same error date: illegal option -- - Usage: date [-u] [+format] date [-u] [mmddhhmm[[cc]yy]] date [-a [-]sss.fff] – Kasiiku Mark Dec 24 '18 at 07:14
  • Ach, you have hp-ux, so you have [this date](http://nixdoc.net/man-pages/HP-UX/date.1.html). There is no `--date` option on your implementation, that is for GNU date. So it would be harder. I say the simpler would be to write a small program (even `perl -e 'print time()'`) for getting seconds since epoch, and using the first method, by subtracting 30 days from it. But if you have perl, you can just use perl. Or write a program in C. – KamilCuk Dec 24 '18 at 09:07