0

I am struggling to get the logs from a file for a given timeframe. I have gone through other posts but everywhere the date/time has been hardcoded, I don't want it to be hardcoded and would like to fetch it programatically.

This is what I have been trying.

This works,

 awk -F, -v b='2016-08-10 00:40:06' -v e='date +"%F %T"' '{ if ($1 >= b && $1 <= e) print}' filename

Whereas this doesn't work,

awk -F, -v b='date --date="10 minutes ago" +"%F %T"' -v e='date +"%F %T"' '{ if ($1 >= b && $1 <= e) print}' filename

Not sure why the first Date parameter is not getting calculated on the fly?

sat
  • 14,589
  • 7
  • 46
  • 65
suraray
  • 3
  • 3

1 Answers1

1

Try something like this:

 awk -v b="`date --date '10 minutes ago'`" 

Example:

date;echo 1 2 3 |awk -v d="`date --date '10 minutes ago'`" '{print d}'
Wed Aug 10 01:57:23 EDT 2016
Wed Aug 10 01:47:23 EDT 2016

echo 1 2 3 | awk -v d="`date --date '10 minutes ago'`" -v e="`date +%F%T`" '{print d, e}'
Wed Aug 10 01:50:05 EDT 2016 2016-08-1002:00:05
P....
  • 17,421
  • 2
  • 32
  • 52
  • In general, it is better to use the `"$(…)"` notation for [command substitution](https://www.gnu.org/software/bash/manual/bash.html#Command-Substitution) than the older ``"`…`"`` notation with back-ticks (which has the additional problem of being hard to enter in Markdown). It's not critical here since there's no nesting, but it is a good idea to use it in general. – Jonathan Leffler Aug 10 '16 at 13:41