-2

Column 13 of my data contains date in YYMMDD format. I'm trying to regex using $date for today and previous days. Neither of the following code would work. Could someone give me some insights?

TODAY

awk -F, ($13~/$(date '+%Y%m%d')/) {n++} END {print n+0}' file.csv)

3 DAYS AGO

awk -F, ($13~/$(date -d "$date -3 days" '+%Y%m%d')/) {n++} END {print n+0}' file.csv
AlphaK12
  • 11
  • 4
  • Welcome to SO, thanks for showing your efforts. Could you please do add samples of input and expected output in your question to make it more clear. – RavinderSingh13 Aug 09 '21 at 04:42
  • as I commented to you on [your previous question](https://stackoverflow.com/a/68689709/10971581), consider: http://stackoverflow.com/q/6697753/10971581 – jhnc Aug 09 '21 at 05:01
  • What date command are you using? On OSX -d sets timezone and -v is used for working out time deltas. So `date '+%Y%m%d'` is now and `date -v-72H '+%Y%m%d'` is 3 days (72 hours) earlier. – MichaelR Aug 09 '21 at 05:13
  • Hello, welcome on SO. The commands you show are not even syntactically correct. Did you try them? – Renaud Pacalet Aug 09 '21 at 06:46

2 Answers2

0

If you are using GNU AWK then you might use its' Time Functions to check if it does work do

awk 'END{print strftime("%y%m%d")}' emptyfile.txt

which should output current day in YYMMDD format. If it does then you might get what you want following way:

awk 'BEGIN{today=strftime("%y%m%d");threedago=strftime("%y%m%d",systime()-259200)}END{print today, threedago}' emptyfile.txt

output (as of today)

210809 210806

Explanation: strftime first argument is time format %y is year 00...99, %m is month 01...12, %d is day 01...31. Second argument is optional, and it is seconds since start of epoch. If skipped current time is used, systime() return number of seconds since start of epoch, 259200 is 72 hours as seconds.

Example of usage as regexp, let say that I have file.txt as follows

210807 120
210808 150
210809 100

and want to retrieve content of 2nd column for today, then I can do

awk 'BEGIN{today=strftime("%y%m%d")}$1~today{print $2}' file.txt

getting output (as of today)

100

(tested in gawk 4.2.1)

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

Your Awk attempts have rather severe quoting problems. You will generally want to single-quote your Awk script, and pass in any parameters as variables with -v.

awk -F, -v when="$(date -d "-3 days" '+%Y%m%d')" '$13~when {n++} END {print n+0}' file.csv

Perhaps notice also that $date is not defined anywhere. The notation $(cmd ...) is a command substitution which runs cmd ... and replaces the expression with its output.

Probably also notice that date -d is a GNU extension and is not portable, though it will work on Linus and other platforms where you have the GNU utilities installed.

More fundamentally, depending on what's in $13, you might want to implement a simple date parsing for that format, so that you can specify a range of acceptable values, rather than search for matches on static text.

This quoting is correct for Bourne-style Unix shells. If you are on Windows, the quoting rules are quite different, and quite likely often impossible to apply in useful ways.

tripleee
  • 175,061
  • 34
  • 275
  • 318