I'm trying to parse a file for some dates in a specific format and get the 'seconds since the Epoch' to calculate absolute differences between the date and the current date. I need to do this on each line using the 4th field, so I tried doing something like this:
"date --date=$4 %s" | getline DUE
close("date --date=$4 %s")
"date %s" | getline CURR
close("date %s")
Of course this doesn't quite work because awk interprets the $4 as a literal "$4". So I can't use variables inside that statement.
I also tried creating 2 variables with the front of the command string and the ending with a third variable being those concatinated with $4 in the middle. That didn't work as well.
I have a feeling you should be able to do such a thing. I know date parses the date just fine. The format is something like 'Thu Aug 11 11:40:00 UTC 2023'.
Is there any way to do this in awk? I've gone through a lot of documentation without ever seeing an example of fields being processed in a shell command.
CodePudding user response:
Use string concatenation.
You also need quotes around the --date
argument, since it contains whitespce.'
command = "date --date=\"" $4 "\" %s"
command | getline DUE
close(command)
command2 = "date %s"
command2 | getline CURR
close(command2)
CodePudding user response:
side note about date
utility ::: the syntax is really different between gnu/linux
one and BSD
ones :
gnu
one allows for fractional epochs for input and output- the
@
is a mandatory requirement for epochs
gdate -d '@16602518201915245' # GNU-date (GNU coreutils 9.1)
Mon Nov 8 22:07:25 EST 526114827
- BSD one won't accept
@
or non-integers
date -r 16602518201915245 # BSD-date (from FreeBSD 12.0)
Mon Nov 8 22:07:25 EST 526114827
they do share one common feature though :
- an upper-limit for the year
-1 2 ^ ( 3 ^ 3 4 )