Alternatively, if your date
doesn't support %s
, with many awk
implementations, you can use srand()
to get the current Unix time:
awk 'BEGIN{srand(); d = srand() - 3 * 30 * 24 * 60 * 60}; $1 > d' < a.txt
Or use perl
:
perl -ne'BEGIN{$d = time - 3 * 30 * 24 * 60 * 60}print if $_ > $d' < a.txt
Note that 3 months here are counted as a fixed period of 90 days of 86400 seconds each.
If instead you want: not before the same day of the month, 3 months ago at the same time of the day (if now is 2013-12-15 21:02:01, that would be since 2013-09-15 21:02:01 (91 days and 1 hour ago in a European Union timezone for instance)), see @Zelda's answer, or:
perl -MPOSIX -ne 'BEGIN{@t=localtime;$t[4]-=3;$d=mktime @t}
print if $_ > $d' < a.txt
(with the caveat that on May 29 (non leap year), May 30 & 31, Jul 31st and Dec 31st, the 3-months ago date will be the same as for GNU date -d '3 months ago'
).
If instead, you mean this month, the previous, or the one before the previous (if today is 2013-12-15, that would be since 2013-10-01 00:00:00), that would be:
perl -MPOSIX -ne 'BEGIN{@t=localtime;$d=mktime 0,0,0,1,$t[4]-2,$t[5]}
print if $_ >= $d' < a.txt
< a.txt
forawk
? – Bernhard Dec 15 '13 at 15:33awk
has some issues with some filenames (those containing=
characters). Having the shell open the file does not have the issue and has other advantages. – Stéphane Chazelas Dec 15 '13 at 20:34awk
on a different fd without "<"), not those reads from the file (awk
in both cases). There could be memory issues if lines are huge (with some awk implementations that don't have a limit there), but that's regardless of whether the shell or awk opens the file. – Stéphane Chazelas Dec 16 '13 at 09:46awk '{}' file
andawk '{}' <file
on a hypothetical huge file (1GB), and did not see noticable differences indeed. – Bernhard Dec 16 '13 at 10:27