Interview Prep/Shell Scripting/Text processing: grep, sed, awk, cut, sort, uniq

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.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Text processing: grep, sed, awk, cut, sort, uniq