Skip site navigation (1)Skip section navigation (2)


| raw e-mail | index | archive | help
yes, indeed, it's like google, know, ask, verify

i tried it too

probably makes crappy mail

and i'm not used to free form natural language search engines like this


for a start - base all you knowledge on https://freebsd=2Eorg/handbook and=
 https://man=2Efreebsd=2Eorg for all commands - now - how do i prefix all o=
utput lines of a command with timestamp


Good =E2=80=94 you=E2=80=99re anchoring everything on authoritative FreeBS=
D docs, which is the right approach=2E
Let=E2=80=99s go straight to your question=2E

=E2=9C=85 Goal

Prefix each line of command output with a timestamp (e=2Eg=2E current date=
/time)=2E

=F0=9F=93=98 On FreeBSD (from sh/bash/zsh/csh)

There are several ways, depending on how precise and flexible you want it=
=2E

1=2E Using awk (simple and portable)

your_command | awk '{ "date +\"[%Y-%m-%d %H:%M:%S]\"" | getline t; close("=
date +\"[%Y-%m-%d %H:%M:%S]\""); print t, $0; fflush(); }'=20

Each line gets a timestamp like:

[2025-10-14 14:37:22] line of output here=20

=F0=9F=92=A1 Explanation:

getline t executes date each time a line arrives=2E

fflush() ensures you see output immediately (no buffering)=2E

2=2E Using ts from moreutils (if installed)

This is the cleanest option if available:

pkg install moreutils your_command | ts '[%Y-%m-%d %H:%M:%S]'=20

Output example:

[2025-10-14 14:37:22] something happened=20

Check if you have it:

which ts=20

3=2E Using perl (no external dependency)

your_command | perl -pe 'chomp($t=3D`date "+[%Y-%m-%d %H:%M:%S]"`); print =
"$t ";'=20

4=2E Using sed and shell substitution (less precise, per run not per line)

If you only need a timestamp at command start, not per line:

your_command | sed "s/^/[$(date '+%Y-%m-%d %H:%M:%S')] /"=20

That inserts the same timestamp on every line (not updating between lines)=
=2E

=E2=9C=85 Recommended for FreeBSD scripts

If ts is available =E2=86=92 use it=2E
If not, prefer the awk version (portable, no extra dependencies)=2E

Would you like the timestamp to include milliseconds too? I can adapt the =
command for that (FreeBSD date supports %N for nanoseconds)=2E




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?>