To perform more accurate time computations, you can use gawk
's time and string functions (per @AdminBee's suggestion). Using input data as:
$ cat data
bob,wag,06/13/1958
ashley,hay,01/23/1983
evan,bert,09/11/1972
You can get a time difference in days between now and the date shown on each line, with:
$ awk -F, 'BEGIN{today=systime()}
{print $0 "," int((today-mktime(substr($3,7,4)" "substr($3,1,2)" "substr($3,4,2)" "00" "00" "00))/(3600*24))}' \
data | tee output-file
bob,wag,06/13/1958,22755
ashley,hay,01/23/1983,13765
evan,bert,09/11/1972,17551
The snippet:
int((today-mktime(substr($3,7,4)" "substr($3,1,2)" "substr($3,4,2)" "00" "00" "00))/(3600*24))
does three basic things for each line of the input file:
- it calculates the time elapsed (in seconds) since 1970-01-01 00:00:00 UTC (on POSIX systems), not counting leap seconds, with
mktime(substr($3,7,4)" "substr($3,1,2)" "substr($3,4,2)" "00" "00" "00)
- it calculates the time difference between the above quantity and the variable
today
, which contains the number of seconds elapsed at time of execution since 1970-01-01 00:00:00 UTC.
- it divides the time difference in seconds by 3600*24 to get the same in days, and only consider the integer part of the result, to get whole days with
int()
.
You could play with that to get your time difference in seconds, minutes, hours per your need. HTH