I ended up writing a quick little script for this in Python, but I was wondering if there was a utility you could feed text into which would prepend each line with some text -- in my specific case, a timestamp. Ideally, the use would be something like:
cat somefile.txt | prepend-timestamp
(Before you answer sed, I tried this:
cat somefile.txt | sed "s/^/`date`/"
But that only evaluates the date command once when sed is executed, so the same timestamp is incorrectly prepended to each line.)
doing it with
date
andtr
andxargs
on OSX:if you want milliseconds:
but note that on OSX, date doesn't give you the %N option, so you'll need to install gdate (
brew install coreutils
) and so finally arrive at this:Disclaimer: the solution I am proposing is not a Unix built-in utility.
I faced a similar problem a few days ago. I did not like the syntax and limitations of the solutions above, so I quickly put together a program in Go to do the job for me.
You can check the tool here: preftime
There are prebuilt executables for Linux, MacOS, and Windows in the Releases section of the GitHub project.
The tool handles incomplete output lines and has (from my point of view) a more compact syntax.
<command> | preftime
It's not ideal, but I though I'd share it in case it helps someone.
Mixing some answers above from natevw and Frank Ch. Eigler.
It has milliseconds, performs better than calling a external
date
command each time and perl can be found in most of the servers.Alternative version with flush and read in a loop:
How about this?
Judging from your desire to get live timestamps, maybe you want to do live updating on a log file or something? Maybe
I'm not an Unix guy, but I think you can use