How to add date string to each line of a continuously written log file

Viewed 24670

Having a long running program that continuously writes to a logfile - how is it possible, disregarding any buffering issues, to add a date string to each line written to that file using a linux script?

I would imagine something like this:

tail -f logfile | ADD_DATE_TO_EACH_LINE > logfile2

The input would be something like that:

abc
def
ghi
jkl

The output should be similar to that:

2011-06-16 18:30:59 abc
2011-06-16 18:31:00 def
2011-06-16 18:35:21 ghi
2011-06-16 18:40:15 jkl
8 Answers

A tool exists for that exact purpose, it’s ts (see man ts)

For example, with your logfile:

tail -f logfile | ts '%Y-%m-%d %H:%M:%S'

Also works with any software writing on the standard output, of course:

./blabla | ts '%Y-%m-%d %H:%M:%S'

If needed, you can add sub-second precision, with %.S instead of %S

I've seen elsewhere a suggestion, if timeliness is important, that one should avoid calling other programs from BASH and use a built-in:

printf '%(%F %T)T\n'

%T is the same format strings from date (see man date), AFAICT. Example:

output="Err, Miss Crabapple, I just ate my worm!"; \
printf '\n%(%F %s)T'; printf "%s \n" " $output"

will output:

2021-03-28 1616894012 Err, Miss Crabapple, I just ate my worm!

I've just noticed a comment on that link has a suggestion to use gawk for timestamp top speeds.

Related