是否有一个Unix实用程序prePEND时间戳标准输入? [英] Is there a Unix utility to prepend timestamps to stdin?

查看:197
本文介绍了是否有一个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.

这篇关于是否有一个Unix实用程序prePEND时间戳标准输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆