How do you parse a log timestamp and filter the last N minutes?
AdvancedConceptText processing: grep, sed, awk, cut, sort, uniq
Answer
Convert both sides to epoch seconds and compare numbers.
since=$(date -d '15 minutes ago' +%s) # GNU date
journalctl -u nginx --since '15 min ago' # easiest when logs are in the journal
# Nginx: [09/Nov/2026:14:23:01 +0000] -> GNU awk mktime
gawk -v since="$since" '
{ split(substr($4, 2), t, /[\/:]/)
m = (index("JanFebMarAprMayJunJulAugSepOctNovDec", t[2]) + 2) / 3
ts = mktime(t[3]" "m" "t[1]" "t[4]" "t[5]" "t[6])
if (ts >= since) print }' /var/log/nginx/access.log
mktime is a gawk extension; mawk on Debian/Ubuntu does not have it. Logging in ISO 8601 (2026-11-09T14:23:01Z) makes this a plain string comparison.
More on Text processing: grep, sed, awk, cut, sort, uniq
What are the most useful `grep` flags?BeginnerWhat is the difference between basic, extended and Perl regular expressions in grep?IntermediateWhat are the core `sed` commands you use in automation?BeginnerWhy does `sed -i` behave differently on macOS and Linux?IntermediateHow do you use variables inside a `sed` expression safely?AdvancedWhat is the structure of an `awk` program?Beginner