我正在使用这个漂亮的单行代码为我的日志文件的每一行添加时间戳:
tail -f log | grep -a --line-buffered "." | awk '{ print strftime("%s\t"), $0; fflush() }'
不幸的是,它只提供完整的秒数,而我需要数据点之间的毫秒分辨率。是否有同样优雅的解决方案来获取 ms 时间戳?我不关心从纪元开始的时间,只关心行之间的差异。
谢谢!
最佳答案
你可以尝试更换grep和 awk与 perl及其 Time::HiRes
内置模块,例如:
tail -f infile | perl -MTime::HiRes=time -ne 'printf "%.3f\t%s", time(), $_'
它产生类似的东西:
1390014680.197 one
1390014680.197 two
1390014680.197 three
1390014680.197 four
1390014680.197 five
1390014680.197 six
1390014689.414 seven
1390014693.542 eight
关于linux - awk strftime 返回时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21199415/