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: sort -m merges several logs into one timeline

Logs
~/tips/tip-logs-sort-merge-by-time

sort -m app.log worker.log | less

Read events from two files in the order they happened.

A web app and its background worker each write their own file, and the bug sits somewhere between them. Reading two files side by side gets old quickly.

When every line starts with a sortable timestamp, such as 2026-11-30T14:05:09, merge them:

sort -m app.log worker.log | less

-m merges files that are already in order, which log files usually are. It does not re-sort them, so it is quick even on large files.

The trouble is that you lose track of which file a line came from. Tag each line with its source, after the timestamp field:

sort -m -k1,1 <(sed 's/ / app /' app.log) <(sed 's/ / worker /' worker.log) | less

The sed inserts a label at the first space, and -k1,1 merges on the timestamp alone. The <( ) form needs bash or zsh.

I find this the cheapest way to see that a request arrived, the worker picked it up and the error came two seconds later.

It only works with timestamps that sort as text. Month names like Nov 3 in classic syslog format do not. Both files should also use the same time zone, or the merged order is wrong while looking perfectly reasonable. Lines without a timestamp, like stack traces, can end up away from their parent line.

Further reading: sort invocation

More Logs tips rotate 4 sed -E 's/[0-9]+/N/g' app.log | sort | uniq -c | sort -rn | head -n 15 awk '$1 >= "2026-11-30T02:00" && $1 < "2026-11-30T03:00"' app.log rotate 14 logger: All Logs tips (9) All topics

Link