brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: awk can cut a time window out of logs that use ISO timestamps

Logs
~/tips/tip-logs-awk-time-window

awk '$1 >= "2026-11-30T02:00" && $1 < "2026-11-30T03:00"' app.log

Extract only the lines between two moments from a large file.

Something broke between two and three at night, and the file covers a month. Searching for the hour as a string gets awkward when the window crosses an hour or a day.

Timestamps in ISO form sort the same way as text, so awk can compare them directly:

awk '$1 >= "2026-11-30T02:00" && $1 < "2026-11-30T03:00"' app.log

This prints each line whose first field falls inside the window. You can give as much or as little of the timestamp as you like. For a date and time in two separate fields, glue them together:

awk '{t = $1 " " $2} t >= "2026-11-29 23:30" && t < "2026-11-30 00:15"' app.log

Save the result and work on that smaller file:

awk '$1 >= "2026-11-30T02:00" && $1 < "2026-11-30T03:00"' app.log > window.log

The thing I like here is that it needs no exact line to match at the start or the end, which a range of two patterns would.

Lines that do not start with a timestamp are the weak spot. A stack trace continuation begins with spaces or a word, and the comparison then gives odd results, either dropping it or including it far outside the window. Check the first and last few lines of the output. Also mind the time zone the program writes in, since UTC logs and your local clock may be an hour or more apart.

Further reading: GNU awk user's guide

More Logs tips rotate 4 sort -m app.log worker.log | less sed -E 's/[0-9]+/N/g' app.log | sort | uniq -c | sort -rn | head -n 15 rotate 14 logger: All Logs tips (9) All topics

Link