是否有一个Unix实用程序prePEND时间戳标准输入?
我结束了在Python写一个快速的小脚本,但我想知道是否有一个实用工具,您可以养活文字到这将prePEND有一些文本每行 - 在我的特定情况下,时间戳。理想的情况下,使用会是这样的:
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
(你回答SED之前,我想这样的:
(Before you answer sed, I tried this:
cat somefile.txt | sed "s/^/`date`/"
不过,只有一次评估时,sed的执行,所以同样的时间戳错误prepended于各行的日期命令)。
But that only evaluates the date command once when sed is executed, so the same timestamp is incorrectly prepended to each line.)
可以尝试使用 AWK
:
<command> | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }'
您可能需要确保&LT;指挥GT;
生产线缓冲的输出,即,它在每行后刷新其输出流;时间戳 AWK
增加将是该行的末尾出现在它的输入管的时间。
You may need to make sure that <command>
produces line buffered output, i.e. it flushes its output stream after each line; the timestamp awk
adds will be the time that the end of the line appeared on its input pipe.
如果AWK显示错误,然后尝试 GAWK
代替。
If awk shows errors, then try gawk
instead.